bash, word splitting, and command substitution

From: Joe Pfeiffer (pfeiffer_at_cs.nmsu.edu)
Date: 12/21/04


Date: 21 Dec 2004 15:00:18 -0700

I would like to use the contents of a file as the command line
arguments to a program, within a bash script. I need word splitting
to occur in the contents I'm pulling from the file, and I'm not
getting the results I expect. I'm using double quote characters in
the file to try to control the word splitting

If the command appears in the script as

   command $(< file)

every word in the file is split -- that is, the quotes in the file are
just treated as ordinary characters, so every word is a separate
argument.

If I try any variation that I've been able to think of for quoting the
command substition like this

    command "$(< file)"

the whole file contents appear as a single argument. I've tried both
single and double quotes.

I've tried every variation on quoting within the file that I can think
of, including single quotes, double quotes, and inhibiting
interpretation of spaces with back-slashes.

It doesn't matter whether I use the form of command substition shown,
actually use a cat command, or use back quotes and the cat command.

Below is a specific script and test data that show the problem.
Instead of the expected (well, I expected it!) behavior showing a
dialog box with two lines of choices, I'm seeing something more like

1 "...
Preshow ..."
\ 2
" 1
Rousing Overture
and Footsteps"

----------------shell script---------------------------
#!/bin/sh
dialog --nocancel --title "Testing..." --menu "Just a Test..." 20 60 10 $(< menulist)

----------------contents of file 'menulist'------------
 1 "... Preshow ..." \
 2 " 1 Rousing Overture and Footsteps" \



Relevant Pages

  • Re: Accessing full command line or parameters WITH QUOTES
    ... The problem is that VBScript doesn't allow access to the full command line ... rebuild the parameters with the quotes inserted properly. ... When I was writing the script on XP, I managed to cheat and grab the ...
    (microsoft.public.scripting.vbscript)
  • Re: Accessing full command line or parameters WITH QUOTES
    ... The problem is that VBScript doesn't allow access to the full command line ... rebuild the parameters with the quotes inserted properly. ... as one argument to your script, ... following will run the php script (assuming php is ...
    (microsoft.public.scripting.vbscript)
  • Re: Accessing full command line or parameters WITH QUOTES
    ... The problem is that VBScript doesn't allow access to the full command line ... rebuild the parameters with the quotes inserted properly. ... what is that escape? ... as one argument to your script, ...
    (microsoft.public.scripting.vbscript)
  • Re: Accessing full command line or parameters WITH QUOTES
    ... rebuild the parameters with the quotes inserted properly. ... command line arguments. ... as one argument to your script, ... It follows that the command processor preserves at least ...
    (microsoft.public.scripting.vbscript)
  • Re: Launch chm file on WS within HTML
    ... escape another. ... However, in Java Script, the rules are ... The command line cd ... |> | and only surrounded the file path with two double quotes. ...
    (microsoft.public.scripting.wsh)