Home > Software engineering >  how can I use lines in a file as arg and append a prefix to each arg in bash?
how can I use lines in a file as arg and append a prefix to each arg in bash?

Time:02-18

I have a file like this:

arg1
arg2
arg3
...
argN

I knew I can use xargs for this cat input.txt | xargs <my command>. However, I would like to append a prefix to each args so the result should be <my command> prefix/arg1 prefix/arg2 ...

The -I cannot work because it implies -L 1 and -x.

CodePudding user response:

You could use sed to change the lines before piping them into xargs

sed 's/^/prefix\//' input.txt | xargs <my command>

CodePudding user response:

As bash is tagged, you could have them as array and substitute the beginning # of each item with your prefix using Shell Parameter Expansion:

mapfile -t args < input.txt
<my command> "${args[@]/#/prefix/}" # <my command> prefix/arg1 prefix/arg2 …
  •  Tags:  
  • bash
  • Related