Single or Double quotes when defining an alias?

6,238

Solution 1

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).

Solution 2

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.

Solution 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.

Share:
6,238

Related videos on Youtube

Greenonline
Author by

Greenonline

Happily Deranged Interested in: Drones 3D Printers (P3Steel v4 and Kossel) Synths and sequencers (CD4017 etc.) Building storage servers UI/UX Macs and iPhone repair Coding/Scripting and Testing software/hardware Hardware: Arduino Pi Macs Z80/68030 OS: iOS MacOS OS X Linux Software/Scripting: C/C++ Java Pascal Assembler (Z80/8086/68k) Perl Python Bourne, bash, csh

Updated on September 18, 2022

Comments

  • Greenonline
    Greenonline over 1 year

    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?

  • Greenonline
    Greenonline about 9 years
    Thanks. I agree that the alias seems a little redundant when the function provided.
  • prosoitos
    prosoitos about 5 years
    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
  • mirageglobe
    mirageglobe about 5 years
    +1 for note on sticking to one and making a habit of a style. its easier to debug.