I'm new to bash so this should be pretty basic but I can't find it so far.

I am going to have an input

cat input_file | sh_file.sh output_file

I want to get the information from the input_file so that I can use the sh_file.sh to pass it on to a python file and do some manipulation and output it from python.

I know that if it was set up like

sh_file.sh input_file output_file

I could just use input_file=$1, output_file=$2 and pass those through to my python file but with the pipe I don't know how to store it in a variable.

  • What exactly is it that you want to store in a variable, the contents of the file, or the name of the file (or something else)? Also, how does the python file handle the input file? – Gordon Davisson Nov 19 '20 at 17:05

Inside the shell script, do this:


that will use the cat command to read standard input and store the result in the $data variable.

Read about the $(...) syntax here: 3.5.4 Command Substitution

  • I thought this worked but it's actually only giving me the first word in my input. If I type cat filename it gives me the correct output, but for some reason using this cuts off all except for the first word – iamjane Nov 19 '20 at 18:37
  • 1
    Exactly how are you using the "$data" variable? I'm guessing that you are using the variable unquoted. – glenn jackman Nov 19 '20 at 19:47
  • That worked! Thank you! – iamjane Nov 19 '20 at 19:51

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.