In a bash script I want to do the following:
script.sh < some_file
The some_file
is a file that has 1 single line that I want to pass it as an argument to my bash script.sh
. How can I do this?
bashshell-script
In a bash script I want to do the following:
script.sh < some_file
The some_file
is a file that has 1 single line that I want to pass it as an argument to my bash script.sh
. How can I do this?
Best Answer
Three variations:
Passing the contents as a command line argument:
Inside the script:
$1
will be the value of the first command line argument.This would fail if you have too much data (the command that the shell would have to execute would grow too big).
Passing the filename:
Inside the script:
or
Connecting standard input to the file:
Inside the script:
The downside with this way of doing it is that the standard input of the script now is connected to
some_file
. It does however provide a lot of flexibility for the user of the script to pass the data on standard input from a file or from a pipeline.