skip to Main Content

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?

2

Answers


  1. You can use $HOME instead of ~.

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search