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.

Share:
26,129

Related videos on Youtube

Chesnokov Yuriy
Author by

Chesnokov Yuriy

Updated on September 18, 2022

Comments

  • Chesnokov Yuriy
    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
    coteyr almost 11 years
    This works too, or theres about 10,000 other ways.
  • Chesnokov Yuriy
    Chesnokov Yuriy almost 11 years
    many 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
    Eric Carvalho almost 11 years
    To pass all the text as single argument you should run ./myapp "$(cat text_file)".
  • Eric Carvalho
    Eric Carvalho almost 11 years
    This will redirect the content of file to the standard input of some_script.sh, not as command line argument.
  • coteyr
    coteyr almost 11 years
    True 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
    Chesnokov Yuriy almost 11 years
    yes, that is the case