hotwired
hotwired

Reputation: 51

Use content of file as part of a Bash command

I want to use the content of a file.txt as part of a bash command. Suppose that the bash command with its options that I want to execute is:

my_command -a first value --b_long_version_option="second value" -c third_value

but the first 2 options (-a and --b_long_version_option ) are very verbose so instead of inserting directly on the command line (or bash script) I wrote them in a file.txt like this:

-a first value \
--b_long_version_option="second value"

Now I expect to call the command "my_command" with the following syntax (where "path_to/file.txt" is the path to file.txt, expressed in relative or absolute form):

my_command "$(cat path_to/file.txt)" -c third_value

This however is not the right syntax, as my command is breaking and complaining.

How should I write the new version of the command and/or the file.txt so that it is equivalent to its native bash usage?

Thanks in advance!

Upvotes: 1

Views: 547

Answers (2)

Paul Hodges
Paul Hodges

Reputation: 15293

The quotes are preserving the newlines. Take them off. You also don't need the cat unless you're running an old bash parser.

my_command $(<path_to/file.txt) -c third_value

You'll need to take the backslashes at the ends of lines out.

Be careful doing things like this, though. It's probably better to just put the whole command in the file, rather than just pieces of it. If you really just want arguments, maybe define them a little more carefully in an array, source the file and then apply them, like this:

in file:

myArgs=( "-a" "first value" 
         "--b_long_version_option=second value"
)

Note the quoting. Then run with

. file
my_command "${myArgs[@]" -c third_value

e.g.,

$: printf "[%s] " "${myArgs[@]}" -c=foo
[-a] [first value] [--b_long_version_option=second value] [-c=foo]

Upvotes: 4

Bahadır Birs&#246;z
Bahadır Birs&#246;z

Reputation: 181

I haven't seen any example of what you're trying. But, there are simpler ways to achieve your goal.

Bash Alias

ll for example is a bash alias for ls -al. It usually is defined in .bash_profile or .bashrc as follows :

alias ll='ls -al'

So, what you can do is to set another alias for your shorthand command.

alias mycmd='mycommand -a first value --b_long_version_option="second value"'

then you can use it as follows :

mycmd -c third_value

Config file

You can also define a mycommand.json file or mycommand.ini file for default arguments. Then, you will need to check for config file in your software, and assign arguments from it.

Using config file is more advanced solution. You can define multiple config files. You can set default config file in /etc/mycommand/config.ini for example. When running on different directories, you should check ${cwd}/mycommand.ini to check local config file exists or not. You can even add a --config-file argument to your command.

Using alias is more convenient for small tasks, or thing that won't change much. If your command's behavior should be different in some other project, the using a config file would be a better solution.

Upvotes: -1

Related Questions