9

I know that contents of double quotes are expanded, whereas the contents of single quotes are not, such that

echo '$1'

gives

$1

where as

echo "$1"

gives

<the argument - blank in this particular example>

the same as

echo $1

However, the question Bash: How to create an alias in .bashrc for awk with parameters led me to wonder why double quotes and not single quotes were used when declaring the alias, thus:

alias cutf="_cutf"

instead of

alias cutf='_cutf'

I would have employed single quotes as in the latter example. Most examples on the web use single too, unless there is some expansion required. However, in this case, no expansion, to my eyes, is apparent. Is it that, in this case, they are interchangeable, or are the double quotes necessary because a function definition is employed?

1
  • 3
    Note that not all expressions are expanded in double quotes; e.g. echo "~" will not be expanded if quoted either way. In double quotes you will get the $-expression expanded. In cases where no expansion is effective but you want no splitting to occur, like in echo "hello world" either quotes are appropriate. In some cases no quotes are necessary (even if often seen), e.g. in assignments like x=value or y=$var.
    – Janis
    Mar 9, 2015 at 17:04

3 Answers 3

15

This is an answer to the question posed by the title (which brought me here), rather than to the OP's particular question.

Single quotes are evaluated dynamically:

alias QS='echo $PWD'

Double quotes are evaluated at time of creation and, thereafter, never changes:

alias QD="echo $PWD"

The same behaviour is found in bash, zsh and, I guess, other shells.

1
  • 1
    and alias QS="echo \$PWD" is the same as alias QS='echo $PWD' since the single quotes turn off the special meaning of all characters and so does escaping a character
    – prosoitos
    Feb 19, 2019 at 21:17
4

In the example you mention, where the expansion for the alias is a single word containing no character that is subject to expansion, it makes no difference how you quote the name _cutf:

$ alias cutf="_cutf"
$ alias cutf
alias cutf='_cutf'

$ unalias cutf
$ alias cutf='_cutf'
$ alias cutf
alias cutf='_cutf'

$ unalias cutf
$ alias cutf=_cutf
$ alias cutf
alias cutf='_cutf'

As you can see in that interaction, recalling the value of the alias cutf gave the same result each time. So, yes, the quoting styles are interchangeable here.

It is not relevant that the expansion is to a function name: at least with bash, aliases perform a simple textual substitution (it is not obvious to me in what way defining an alias is useful; directly calling the underlying function seems just as easy).

1
  • Thanks. I agree that the alias seems a little redundant when the function provided. Mar 10, 2015 at 2:35
3

It's just that simple - if there's no variable, the brackets are interchangeable. Shell script, while very useful, is also a very simple language. I (and some others I know) tend to use double quotes by default, just from force of habit - and I have never run into issues.

1
  • +1 for note on sticking to one and making a habit of a style. its easier to debug. Mar 2, 2019 at 23:29

You must log in to answer this question.

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