Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am storing a file path in a file and need to pass the contents of that file as an argument to a shell script, specifically Maven, something like so:

mvn -Dvar_name=(contents of file)

Would this work:

mvn -Dvar_name=(cat /path/to/file)


share|improve this question
up vote 8 down vote accepted

This can be done using command substitution, like so:

mvn -Dvar_name="$(cat /path/to/file)" # POSIX
mvn -Dvar_name="$(</path/to/file)"    # bash

This has a notable caveat, though, namely that all trailing newlines are stripped. If that doesn't matter, though, then that should work.

If you really just want to read one line, you could use read instead, like so:

IFS= read -r line < /path/to/file
mvn -Dvar_name="$line"
share|improve this answer

For command substitution, you need to use $() or backticks ``.

It is also important that you quote the substitution, or it will expand into multiple arguments if the file contains more than one word. Here are some examples:

mvn -Dvar_name="$(< /path/to/file)" # bash

mvn -Dvar_name="$(cat /path/to/file)" # POSIX
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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