Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

2 Answers 2

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

 
discard

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.