Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

14
  • 1
    Note that in csh, you'd rather use $var:q than "$var" as the latter doesn't work for variables that contain newline characters (or if you want to quote the elements of the array individually, rather than join them with spaces into a single argument). Commented Feb 8, 2018 at 16:38
  • In bash, bitrate="--bitrate 320" works with ${bitrate:+$bitrate}, and bitrate=--bitrate 128 won't work ${bitrate:+"$bitrate"} because it breaks the command. Is is safe to use ${variable:+$variable} with no "? Commented Jun 7, 2019 at 7:14
  • @Freedo: I find your comment unclear.  I’m especially unclear what you want to know that I haven’t already said.  See the second “But what if” heading — “But what if I have a variable that I want/need to be split into words?”  That’s your situation, and you should be following the advice there.  But if you can’t (e.g., because you’re using a shell other than bash, ksh, zsh or yash, and you’re using $@ for something else), or you simply refuse to use an array for other reasons, I refer you to “As discussed in the other answer”.  … (Cont’d) Commented Jun 7, 2019 at 19:15
  • (Cont’d) …  Your suggestion (using ${variable:+$variable} with no ") will fail if IFS contains -, 0, 2, 3, a, b, e, i, r or t. Commented Jun 7, 2019 at 19:15
  • 1
    See Understanding IFS, What is the 'IFS'?, Understanding the default value of IFS, Setting IFS for a single statement and Split string using IFS.  Also related: Why does my shell script choke on whitespace or other special characters?   (And did you read all the answers to this question?) Commented Jun 9, 2019 at 17:37