0

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?

  • Show that part of the bash script which runs _xdg-open_. Insert into this script, right before invoking _xdg-open_, a `echo version=$BASH_VERSION` and post what you get as output. – user1934428 Oct 24 '22 at 10:16
  • Hard to give you an answer. The behavior you describe is not true. ~ could change expansion if HOME is redefined, but not that way. So, the real question is, why are you under the false impression it is so. Hard to tell that without seeing the script. – chrslg Oct 24 '22 at 10:34
  • Hello @user1934428, After running this, the script outputs `version=5.2.2(1)-release`. – DabiaTheNord Oct 24 '22 at 10:37
  • Don't put code into a comment. It's hard to read, since we don't see where a line ends. You can edit your posting by adding the necessary version. – user1934428 Oct 24 '22 at 10:39
  • Sorry about that, I didn't know comments didn't support full codeblocks, I've edited my question to include the full script. – DabiaTheNord Oct 24 '22 at 10:43
  • Sounds like your question is really about `zenity`, since that seems to be the program generating the path. – William Pursell Oct 24 '22 at 12:33

2 Answers2

0

You can use $HOME instead of ~.

Son Tran Thai
  • 251
  • 2
  • 7
0

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.

user1934428
  • 19,864
  • 7
  • 42
  • 87
  • I previously tried with and without double-quotes and the result was the same, but I will keep this in mind. The closest I was able to get so far is to change directory to `~` before running `xdg-open` – DabiaTheNord Oct 24 '22 at 10:57
  • Without double quote would make things even worse, because it would break on directory names containing a space. However, you are right, that I forgot that tilde expansion also does not work in unquoted parameters (I will edit my answer in this respect). In theory, `$(eval "$input")` would work, but **this** is certainly a risk you should not take. – user1934428 Oct 24 '22 at 11:01
  • @DabiaTheNord: BTW, you can find the expansion order for bash by checking the chapter titled _EXPANSION_ in the man page. You can see that tilde expansion is happening pretty early in the list. – user1934428 Oct 24 '22 at 11:05
  • as strange as it sounds, the `$(eval "$input")` is the closest I've gotten so far, however it's giving me the `/home/dabiathenord: Is a directory` message, followed by the synopsis for xdg-open. – DabiaTheNord Oct 24 '22 at 11:20
  • In any case, you would certainly not do an `eval` with tainted data. Aside from this, the expansion does not look unreasonable to me. What would you have expected for your homedirectory? – user1934428 Oct 24 '22 at 12:07