Is there a way in bash that we can properly use the output of command substitution (` COMMAND ` or $(COMMAND)) as arguments for another program?


% touch -- safe-name -name-with-dash-prefix "name with space" \
    'name-with-double-quote"' "name-with-single-quote'" \

Neither this:

% ls -l `ls`
% ls -l $(ls)

nor this:

% ls -l "`ls`"
% ls -l "$(ls)"

can properly re-feed the output of the command substitution as arguments, for all the sample names. Without double quote, names with quotes are okay but not names with spaces. With double quote, name with spaces is okay but multiple names are joined together.

Note: please limit discussion to command subtitution (i.e. no pipes, mapfile, or xargs).

  • 1
    I set the IFS to newline in this case to handle most cases (but not file names with a newline) – Philippos Aug 11 '17 at 5:49
  • @Philippos: that handles wordsplitting (except newline as you say) but not globbing so also do set -f or equivalent – dave_thompson_085 Aug 11 '17 at 7:53
  • Yes, but I see no globbing here – Philippos Aug 11 '17 at 21:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.