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


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

Re: correct quoting for variables in bash scripts?

Path csiph.com!news.mixmin.net!news2.arglkargh.de!news.karotte.org!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From Ralf Fassel <ralfixx@gmx.de>
Newsgroups comp.os.linux.misc
Subject Re: correct quoting for variables in bash scripts?
Date Mon, 17 Oct 2022 14:15:10 +0200
Lines 33
Message-ID <ygamt9u8x4h.fsf@akutech.de> (permalink)
References <tije9a$19dt$1@gioia.aioe.org>
Mime-Version 1.0
Content-Type text/plain
X-Trace individual.net hY+z9+K3lV+0gT2YZMXm7gngBtpsmLqyRQRnBVwKzKURO3Qgc=
Cancel-Lock sha1:t/ZyxQuC/AyTDtsjPUx2dIGS8FA= sha1:XfbJoZ8THXb2e0V/BnfGNeowsWM=
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/25.3 (gnu/linux)
Xref csiph.com comp.os.linux.misc:35734

Show key headers only | View raw


* Barry Stir <scooter@mealteam6.mil>
| 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...

Using "" also works if the variable is not set or empty, otherwise you
get a syntax error in that case.

Usually you need {} only when additional text should be appended to the
contents: ${LINK1_DIR}X (because without the {} the variable name would
be LINK1_DIRX).

| So which is the ideal extra character to put around the environment
| variable?

It depends :-)

"" also prevents file name globbing from happening, which might
or might not be what one wants.

    $ touch foo1 foo2
    $ a=foo\*
    $ echo $a
    foo1 foo2
    $ echo "$a"
    foo*

R'

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