Home > OS >  Bash script paths behaving differently from command-line paths
Bash script paths behaving differently from command-line paths

Time:10-24

currently what I'm trying to do in Bash on my Ubuntu machine is utilize xdg-open to find a certain path or file.

In the command line, here's what typically happens;

When I run xdg-open ~/Downloads/, This opens the file manager in the ~/Downloads/ folder, regardless of where my current directory is, as it should.

However, when I run xdg-open ~/Downloads/ in a bash script, it attempts to read from the script's path and the path provided, which results in something similar to xdg-open /path/of/my/script/~/Downloads/, which I don't want.

My current script looks a bit like this;

#!/usr/bin/env bash

input=$(zenity --entry --text="Enter the URL or file." --title=Run --window-icon=question)

echo version=$BASH_VERSION
xdg-open "$input"

exit

How could I make it so my Bash script's xdg-open line behave how it does in the command line?

CodePudding user response:

You can use $HOME instead of ~.

CodePudding user response:

Tilde-expansion is done before parameter expansion. Hence the tilde is not expanded and "$input" is treated as a relative path.

You could expand your ~ manually inside the variable.

An alternative to consider would be to rewrite your shell script in zsh, where you could write ${~input} to cause tilde expansion in parameters.

  • Related