0

I am using nautilus file manager and created the file ~/.bash_aliases with the line

alias naut="nautilus $(pwd) >/dev/null 2>&1 & disown;"

Then I execute

source ~/.bashrc

Then when I type the command naut, the file manager does not open in the pwd directory but in the directory in which I last executed source ~/.bashrc . Which is weird, because when I create an alias

alias naut="pwd; nautilus $(pwd) > /dev/null 2>&1 & disown;"

the command naut always returns my actual working directory, while opening nautilus in another directory.

How is this possible?

2
  • 3
    It is quite clear: $(pwd) is expanded at the time the alias command containing it is run. Try enclosing the alias in single, instead of double, quotes.
    – fkraiem
    Dec 15, 2016 at 15:42
  • 3
    Or just use nautilus .
    – user423626
    Dec 15, 2016 at 16:28

1 Answer 1

3

man bash:

Enclosing characters in double quotes preserves the literal value of all characters within the quotes, with the exception of $, `, \, and, when history expansion is enabled, !. The characters $ and ` retain their special meaning within double quotes. The backslash retains its special meaning only when followed by one of the following characters: $, `, ", \, or <newline>. A double quote may be quoted within double quotes by preceding it with a backslash. If enabled, history expansion will be performed unless an ! appearing in double quotes is escaped using a backslash. The backslash preceding the ! is not removed.

As a consequence, in your definition $(pwd) is expanded when the alias is defined, not when it is executed. Use single quotes around the right-hand side of the alias definition.

On the other hand, as Bharadwaj Raju observes, the current working directory is always ., so nautilus "$(pwd)" is the same as nautilus .. And you probably want doublequotes around $(pwd).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .