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)



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"

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

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.