I am running a bash script with its parameters/arguments as files. From inside the script, how do I get the contents of those files?

I run the script with this command line:

sh script_name animal.txt stars.txt

animal.txt contains "giraffe".

From the script, how do I get the contents (giraffe) of animal.txt? I am very new to bash scripts, so any help is appreciated, thanks.

  • If it's a bash script why do you call it as sh ... and not as bash ...? Jan 20, 2015 at 3:34
  • I didn't know that there was a difference. My testing up until this point has been OK using sh. I'll start using bash though. Anyway, could you help with my problem?
    – eltigre
    Jan 20, 2015 at 3:36
  • Certain features don't work if bash is called as sh. As long as you don't use the bash-specific features (which is typical for beginners) both ways work. Jan 20, 2015 at 3:40

3 Answers 3

for f do exec <"$f"
    : handle stdin

A non-interactive shell will treat any redirection from a file that cannot be read or that does not exist when associated w/ a special builtin as a fatal error and exit immediately with a meaningful diagnostic message written to stderr. So either your parameters are valid, readable files and the above statement will do nothing useful until you replace the : ... part w/ something useful, or the user has provided an invalid parameter and the script exits meaningfully.

When you do...

for var in ...; do : compound command list on "$var"; done

The in ...; bit is an optional statement in the syntax which enables you to substitute a parameter set for the default set - which is your argument list.


for var do : compound command list on "$var"; done

...is probably what you're looking for, here.

for f do exec <"$f"
    : now do some stuff w/ stdin

...where the shell will iteratively assign to stdin and validate as readable files any arguments provided your script for all commands that follow the exec statement until all parameters are exhausted and the for loop is done. If it cannot do this then it will format and print your error message for you in a standard form to which the user is already accustomed.

  • "A non-interactive shell will treat any redirection from a file that cannot be read or that does not exist as a fatal error and exit immediately" – Have you tried that? I don't think so. Jan 20, 2015 at 7:21
  • Thanks for the reply. What does $@ do? Googling doesn't give any results.
    – eltigre
    Jan 20, 2015 at 7:49
  • @eltigre: See the Special Parameters section in the bash man page.
    – PM 2Ring
    Jan 20, 2015 at 10:46
  • 1
    @HaukeLaging - Dang - yes and no. It's for special builtins. I was tired when I wrote that. It is now fixed.
    – mikeserv
    Jan 20, 2015 at 14:58
  • @eligre - a better reference is here. I also wrote an answer yesterday or the day before related to the shell array "$@".
    – mikeserv
    Jan 20, 2015 at 15:32
test -f "$first_file" || exit 2
echo "$file_content"
  • So I assume file_contents will be a string. Would concatenating it like: a=$file_content$anotherstring work?
    – eltigre
    Jan 20, 2015 at 3:55
  • @eltigre Yes. But even though it is not necessary in this case you should get used to quote everything: a="${file_content}${anotherstring}" Jan 20, 2015 at 4:08
  • Note that file_content will contain the content of $first_file stripped of all trailing newline characters provided there is no NUL characters in there. Jan 20, 2015 at 15:03

You can do it:

while read line ;
    echo "$line"
done < "$1"

description: each $line is a content of complete line of your file.

usage : ./script.sh file.txt

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .