Bash script variable syntax: with some commands it works, with others it does not

I do not understand the behaviour of this variable:

SSH_CONFIG_FILE="~/.ssh/config"
echo $SSH_CONFIG_FILE
ls -l $SSH_CONFIG_FILE
ls -l ~/.ssh/config

This is the output:

~/.ssh/config
ls: cannot access '~/.ssh/config': No such file or directory
-rw------- 1 pm domain^users 1229 Sep 19 10:52 /home/pm/.ssh/config

Why does echo work with the $ notation, and ls does not?

I tried surrounding the variable with "", ”, “, {}, [] with no improvement.

Asked By: Pietro

||

Don’t put quotes around tilde ~ if you expect the shell to expand it to your home, so:

SSH_CONFIG_FILE=~/.ssh/config

if you have space(s); do

var=~/'foo bar/file'

With the quotes on tilde, you prevent the shell to expand it to treat literally.

Answered By: Gilles Quénot
Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.