Add command arguments using inline if-statement in bash

A more idiomatic version of svlasov’s answer:

ls $( (( test == 1 )) && printf %s '-la' ) -R

Since echo understands a few options itself, it’s safer to use printf %s to make sure that the text to print is not mistaken for an option.
Note that the command substitution must not be quoted here – which is fine in the case at hand, but calls for a more robust approach in general – see below.

However, in general, the more robust approach is to build up arguments in an array and pass it as a whole:

# Build up array of arguments...
args=()
(( test == 1 )) && args+=( '-la' )
args+=( '-R' )

# ... and pass it to `ls`.
ls "${args[@]}"

Update: The OP asks how to conditionally add an additional, variable-based argument to yield ls -R -la "$PWD".
In that case, the array approach is a must: each argument must become its own array element, which is crucial for supporting arguments that may have embedded whitespace:

(( test == 1 )) && args+= ( '-la' "$PWD" ) # Add each argument as its own array element.

As for why your command,

 ls `if [ $test -eq 1 ]; then -la; fi` -R

didn’t work:

A command between backticks (or its modern, nestable equivalent, $(...)) – a so-called command substitution – is executed just like any other shell command (albeit in a sub-shell) and the whole construct is replaced with the command’s stdout output.

Thus, your command tries to execute the string -la, which fails. To send it to stdout, as is needed here, you must use a command such as echo or printf.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)