How to pass text file contents as argument to console application?
26,129
Solution 1
./myapp `cat text_file`
Or
./myapp $(cat text_file)
Or use double quotes to pass all the text as a single argument
./myapp "$(cat text_file)"
./myapp "`cat text_file`"
Solution 2
Very simple, cat it.
cat file | some_script.sh
Have a look here for further help.
Related videos on Youtube
Author by
Chesnokov Yuriy
Updated on September 18, 2022Comments
-
Chesnokov Yuriy over 1 year
What is the command line to run a console application with input argument fed from text file?
text_file: This is a simple text file with characters and other symbols including tabs and new lines
The console should get
$./myapp "This is a simple text file with characters and other symbols including tabs and new lines"
-
coteyr almost 11 yearsThis works too, or theres about 10,000 other ways.
-
Chesnokov Yuriy almost 11 yearsmany thanks, let me try them. will the contents be passed in quotes? to treat them as a single argument since there are spaces in text file
-
Eric Carvalho almost 11 yearsTo pass all the text as single argument you should run
./myapp "$(cat text_file)"
. -
Eric Carvalho almost 11 yearsThis will redirect the content of file to the standard input of some_script.sh, not as command line argument.
-
coteyr almost 11 yearsTrue but the question was "with input argument fed from text file" and if some_script.sh takes stdin then the "effect" is the same without having to worry about argument length issues or escaping.
-
Chesnokov Yuriy almost 11 yearsyes, that is the case