Attach Arguments - Escape Spaces

Is there an easy way to "join" the arguments passed to the script? I would like something similar to $@ , but this immediately passes the arguments.

For example, consider the following script:

 $/bin/bash ./my_program $@ 

On call

 ./script arg1 arg2 arg3 

my_program will receive the arguments as 3 separated arguments. I want to pass all arguments as one argument, combining them - separated by spaces, something like a call:

 ./my_program arg1\ arg2\ arg3 
+7
source share
2 answers

Use this:

 ./my_program "$*" 
+13
source

Today I had a pretty similar problem, and I came up with this code (including health checks):

 #!/bin/sh # sanity checks if [ $# -lt 2 ]; then echo "USAGE : $0 PATTERN1 [[PATTERN2] [...]]"; echo "EXAMPLE: $0 TODO FIXME"; exit 1; fi NEEDLE=$(echo $* | sed -s "s/ /\\\|/g") grep $NEEDLE . -r --color=auto 

This small script allows you to search for recursive words in all files below the current directory. You can use sed to replace your separator "(space) with whatever you like. I replace the space with a shielded tube:" "->" \ | "The resulting string can be analyzed by grep.

Using

Find the terms "TODO" and "FIXME":

 ./script.sh TODO FIXME 

You may wonder: why not just call grep directly?

 grep "TODO\|FIXME" . -r 

The answer is that I wanted to do some post processing with the results in a more advanced script. Therefore, I needed to compress all the transferred arguments (templates) into one line. And here is the key part here.

Konrad

+3
source

All Articles