All Questions
32
questions
148
votes
11
answers
55k
views
What is the difference between $* and $@?
Consider the following code:
foo () {
echo $*
}
bar () {
echo $@
}
foo 1 2 3 4
bar 1 2 3 4
It outputs:
1 2 3 4
1 2 3 4
I am using Ksh88, but I am interested in other common ...
52
votes
3
answers
75k
views
Add arguments to 'bash -c'
Let's say that I want to run a command through Bash like this:
/bin/bash -c "ls -l"
According to Bash man page, I could also run it like this:
# don't process arguments after this one
...
21
votes
3
answers
16k
views
How does 'find -exec' pass file names with spaces?
If I have a directory containing some files whose names have spaces, e.g.
$ ls -1 dir1
file 1
file 2
file 3
I can successfully copy all of them to another directory like this:
$ find dir1 -mindepth ...
18
votes
2
answers
12k
views
Bash string concatenation used to build parameter list
Given this piece of bash:
PARMS='-rvu'
PARMS+=" --delete --exclude='.git'"
echo $PARMS
rsync ${PARMS} . ${TARGET}
The echo shows the PARMS string as expected, no error is displayed, but rsync ...
14
votes
4
answers
19k
views
How to prevent command injection through command options?
I have an wrapper application where I need to let the user specify custom options to pass to a simulator. However, I want to make sure the user doesn't inject other commands through the user options. ...
12
votes
2
answers
23k
views
Pass arguments to function exactly as-is
I have the following function:
bar() { echo $1:$2; }
I am calling this function from another function, foo. foo itself is called as follows:
foo "This is" a test
I want to get the ...
9
votes
4
answers
3k
views
Can a shell script prints its argument, quoted as you would write them on the shell prompt?
In a shell script, my understanding is that "$@" expands to the script arguments, quoting them as needed. For instance this forwards the script arguments to gcc:
gcc -fPIC "$@"
When using the bash ...
7
votes
3
answers
2k
views
Bash : command line with optional arguments
I'm running this kind of code:
#!/usr/bin/env bash
set -u
exclude1='--exclude=/path/*'
exclude2='--exclude=/path with spaces/*'
exclude3='' # any 'exclude' can be empty
tar -czf backup.tgz "$...
5
votes
3
answers
1k
views
Find exec sh: Shell variable not getting passed to subshell
Here is a simplified code that prints the name of Directory if it contains a Filename with same name as the parent directory and .md extension.
FIND(){
find . -type d -exec sh -c '
for d ...
5
votes
2
answers
4k
views
Bash: passing braces as arguments to bash function
I love using the following pattern for searching in files:
grep --color=auto -iRnHr --include={*.js,*.html,} --exclude-dir={release,dev,} "span" .
I'd like, however, to have this one wrapped into a ...
5
votes
2
answers
2k
views
How to re-write this function to avoid argument injection
I have a function in my .bashrc file that allows me to run a script on a remote server with arguments via ssh.
Currently, the function contains:
function runMyScript {
if [ $1 = "s3" ]
then
...
4
votes
4
answers
3k
views
Correctly quote array that is being passed indirectly via another command
I need to pass an array of filenames to a command, preserving proper quoting. So far, so good. Unfortunately the command is actually a sub-command that is, in turn, invoked by another command. ...
3
votes
1
answer
248
views
tcsh: Handle spaces in arguments when passing on to another command
I wrote a script that needs to call a command and pass on arguments. My script has its own parameters but some need to be passed through. This fails when arguments to my script have spaces in them.
...
3
votes
1
answer
6k
views
Passing arguments with spaces and quotes to a script (without quoting everything)
The following works great on the command-line:
$ ffmpeg -i input.m4a -metadata 'title=Spaces and $pecial char'\''s' output.m4a
How do I parameterize this command and use it in a script/function? I ...
3
votes
2
answers
10k
views
Passing paths with spaces as arguments
I am having difficulty in passing some string variables having spaces in them as arguments to a program.
For debugging and showing the arguments being passed, I created a demo Python script -:
#####...