Command line arguments from file contents

How to convert file contents to argument for Unix command?

The inclusion of the argument in the contents of the file is as follows:

echo ABC > file.txt 

But another direction?

+85
command-line linux unix shell command-line-arguments
Nov 19 2018-10-18
source share
7 answers

If your shell is bash (among others), the shortcut for $(cat afile) is $(< afile) , so you should write:

 mycommand "$(< file.txt)" 

It is documented in the bash man page in the "Replacing Commands" section.

Otherwise, try reading the stdin command, therefore: mycommand < file.txt

+121
Nov 19 '10 at 20:35
source share

As already mentioned, you can use backticks or $(cat filename) .

What was not mentioned, and I think it is important to note that you must remember that the shell will break the contents of this file according to a space, providing each "word" that it finds in your command as an argument. And although you can enclose the command line argument in quotation marks so that it can contain spaces, escape sequences, etc., Reading from a file will not do the same. For example, if your file contains:

 a "bc" d 

the arguments you get are:

 a "b c" d 

If you want to output each line as an argument, use the while / read / do construct:

 while read i ; do command_name $i ; done < filename 
+29
Nov 19. '10 at 21:40
source share

You do this with backticks:

 echo World > file.txt echo Hello `cat file.txt` 
+13
Nov 19 '10 at 18:10
source share
 command `< file` 

will pass the contents of the file to the stdin command, but will split the lines, which means that you cannot iterate over each line individually. To do this, you can write a script with a 'for' loop:

 for i in `cat input_file`; do some_command $i; done 
+8
Nov 19 '10 at 21:05
source share

If you want to do this in a reliable way that works for every possible command line argument (values ​​with spaces, values ​​with newlines, values ​​with literal quotation marks, values ​​that cannot be printed, values ​​with glob characters, etc.), it gets a little more interesting.




To write to a file, specify an array of arguments:

 printf '%s\0' "${arguments[@]}" >file 

... replace with "argument one" , "argument two" , etc., if necessary.




To read from this file and use its contents (in bash, ksh93 or another recent shell with arrays):

 declare -a args=() while IFS='' read -r -d '' item; do args+=( "$item" ) done <file run_your_command "${args[@]}" 



To read from this file and use its contents (in a shell without arrays, note that this will overwrite your local list of command line arguments, and thus it is best done inside the function, so that you overwrite the function with arguments, not the global list) :

 set -- while IFS='' read -r -d '' item; do set -- "$@" "$item" done <file run_your_command "$@" 

Note that -d (which allows you to use a different line terminator) is an extension other than POSIX, and a shell without arrays cannot support it either. In this case, you may need to use a language without a shell to convert NUL-restricted content to an eval safe form:

 quoted_list() { ## Works with either Python 2.x or 3.x python -c ' import sys, pipes, shlex quote = pipes.quote if hasattr(pipes, "quote") else shlex.quote print(" ".join([quote(s) for s in sys.stdin.read().split("\0")][:-1])) ' } eval "set -- $(quoted_list <file)" run_your_command "$@" 
+5
Mar 19 '14 at 20:59
source share

This is how I pass the contents of the file as an argument to the command:

 ./foo --bar "$(cat ./bar.txt)" 
+4
Jan 10 '16 at 0:54
source share

In my bash shell, the following worked like a charm:

 cat input_file | xargs -I % sh -c 'command1 %; command2 %; command3 %;' 

where input_file

 arg1 arg2 arg3 

As is obvious, this allows you to execute several commands with each line from input_file, a nice little trick I learned here .

+3
Mar 19 '14 at 20:57
source share



All Articles