Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.os.linux.misc > #35736

Re: correct quoting for variables in bash scripts?

From Rich <rich@example.invalid>
Newsgroups comp.os.linux.misc
Subject Re: correct quoting for variables in bash scripts?
Date 2022-10-17 14:02 +0000
Organization A noiseless patient Spider
Message-ID <tijn9n$3f5pf$2@dont-email.me> (permalink)
References <tije9a$19dt$1@gioia.aioe.org>

Show all headers | View raw


Barry Stir <scooter@mealteam6.mil> wrote:
> I see various formats, such as
>   if [[ -L ${LINK1_DIR} ]]; then
> 
>   if [[ -L "$LINK1_DIR" ]]; then
> 
> rather than just $LINK1_DIR
> I presume this is insurance to ensure script works in every version of 
> Red Hat, Debian, Arch whatever...
> So which is the ideal extra character to put around the environment 
> variable?

The ${} form has a different meaning to the quotes (i.e., they do 
different things).

The ${} form is defined in the bash man page this way:

       ${parameter}
              The  value of parameter is substituted.  The braces are 
              required when parameter is a positional parameter with 
              more than one digit, or when parameter is followed by a 
              character which is not to be interpreted as part of its 
              name.

So the ${} form provides a way to delimit exactly the name of the 
variable, without confusing it with further concatenated text.

The double quotes "" are for preventing the interpretation of special 
characters that might be stored in the variable.  Double qutoes are 
defined this way in the bash man page:

       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.

So, if one is not appending text to the variable, one only needs to do:

"$LINK1_DIR"

The {}'s are redundant in that case.  However there are some folks who 
seem to like the look of ${...} and always include the {}'s.

The double quotes, however, are in most cases, a very good idea to 
always include, because if you leave them off, and one day the variable 
contains a space (or other character special to the shell) then things 
will break, often badly.

Back to comp.os.linux.misc | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

correct quoting for variables in bash scripts? Barry Stir <scooter@mealteam6.mil> - 2022-10-17 19:28 +0800
  Re: correct quoting for variables in bash scripts? Aragorn <telcontar@duck.com> - 2022-10-17 14:10 +0200
  Re: correct quoting for variables in bash scripts? Ralf Fassel <ralfixx@gmx.de> - 2022-10-17 14:15 +0200
    Re: correct quoting for variables in bash scripts? Jerry Peters <jerry@example.invalid> - 2022-11-01 20:27 +0000
  Re: correct quoting for variables in bash scripts? "Carlos E. R." <robin_listas@es.invalid> - 2022-10-17 14:39 +0200
  Re: correct quoting for variables in bash scripts? Rich <rich@example.invalid> - 2022-10-17 14:02 +0000
  Re: correct quoting for variables in bash scripts? Henning Hucke <h_hucke+spam.news@newsmail.aeon.icebear.org> - 2022-10-17 13:54 +0000
  Re: correct quoting for variables in bash scripts? Eli the Bearded <*@eli.users.panix.com> - 2022-10-17 23:35 +0000
    Re: correct quoting for variables in bash scripts? Ralf Fassel <ralfixx@gmx.de> - 2022-10-18 11:31 +0200
      Re: correct quoting for variables in bash scripts? Rich <rich@example.invalid> - 2022-10-18 12:02 +0000
        Re: correct quoting for variables in bash scripts? Ralf Fassel <ralfixx@gmx.de> - 2022-10-18 15:01 +0200
          Re: correct quoting for variables in bash scripts? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2022-10-18 14:17 +0000
            The difference between /bin/test and /bin/[ Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2022-10-18 14:58 +0000
              Re: The difference between /bin/test and /bin/[ Eli the Bearded <*@eli.users.panix.com> - 2022-10-21 04:41 +0000
            Re: correct quoting for variables in bash scripts? Ralf Fassel <ralfixx@gmx.de> - 2022-10-19 12:04 +0200

csiph-web