Skip to main content
128 votes

Multiple arguments in shebang

Although not exactly portable, starting with coreutils 8.30 and according to its documentation you will be able to use: #!/usr/bin/env -S command arg1 arg2 ... So given: $ cat test.sh #!/usr/bin/env ...
unode's user avatar
  • 1,740
112 votes

Pass the output of previous command to next as an argument

I tend to use this: command1 | xargs -I{} command2 {} Pass output of command1 through xargs using substitution (the braces) to command2. If command1 is find be sure to use -print0 and add -0 to xargs ...
Michael Anderson's user avatar
86 votes
Accepted

How does curl protect a password from appearing in ps output?

When the kernel executes a process, it copies the command line arguments to read-write memory belonging to the process (on the stack, at least on Linux). The process can write to that memory like any ...
Gilles 'SO- stop being evil''s user avatar
58 votes
Accepted

Why do I need to use cd "$@" instead of cd "$1" when writing a wrapper for cd?

Because, according to bash(1), cd takes arguments cd [-L|[-P [-e]] [-@]] [dir] Change the current directory to dir. if dir is not supplied, ... so therefore the directory ...
thrig's user avatar
  • 35.8k
57 votes
Accepted

Why is `sort < "$f1" ` preferred over `sort -- "$f1"`, and why is this preferred over `sort "$f1"`?

sort "$f1" fails for values of $f1 that start with - or here for the case of sort some that start with + (can have severe consequences for a file called -o/etc/passwd for instance). sort -- "$f1" (...
Stéphane Chazelas's user avatar
54 votes

Solving "mv: Argument list too long"?

If working with Linux kernel is enough you can simply do ulimit -S -s unlimited That will work because Linux kernel included a patch around 10 years ago that changed argument limit to be based on ...
Mikko Rantalainen's user avatar
51 votes
Accepted

Multiple arguments in shebang

There is no general solution, at least not if you need to support Linux, because the Linux kernel treats everything following the first “word” in the shebang line as a single argument. I’m not sure ...
Stephen Kitt's user avatar
47 votes
Accepted

Why does rm manual say that we can run it without any argument, when this is not true?

The standard synopsis for the rm utility is specified in the POSIX standard1&2 as rm [-iRr] file... rm -f [-iRr] [file...] In its first form, it does require at least one file operand, but in its ...
Kusalananda's user avatar
  • 356k
36 votes
Accepted

Why doesn't using multiple commands with a || or && conditional work?

Note that your line [ "${#}" -eq 1 ] || echo "Invalid number of arguments, expected one."; exit 1 this is the same as [ "${#}" -eq 1 ] || echo "Invalid number of arguments, expected one." exit 1 (an ...
Kusalananda's user avatar
  • 356k
33 votes
Accepted

How can I conditionally pass an argument from a POSIX shell script?

Modifying your original script: #!/bin/sh echo "Noise $1" echo "Enhancement $2" for snr in 0 5 10 15 20 25 do python evaluate.py --noise "$1" --snr "$snr" --iterations 1250 ${2:+--enhancement "$2"} ...
Kusalananda's user avatar
  • 356k
31 votes
Accepted

Why does position of -C matter in git commands?

This is because -C is a global option, and doesn't "belong" to the status action. This is a common pattern, resulting in synopses like the one below: command [global options] action [action-...
l0b0's user avatar
  • 53.6k
26 votes
Accepted

Bash getopts, mandatory arguments

Maybe something like this? #!/bin/bash unset -v host unset -v port unset -v user while getopts h:p:u: opt; do case $opt in h) host=$OPTARG ;; p) port=$OPTARG ;...
Kusalananda's user avatar
  • 356k
25 votes
Accepted

Hide arguments to program without source code

As explained here, Linux puts a program's arguments in the program's data space, and keeps a pointer to the start of this area. This is what is used by ps and so on to find and show the program ...
meuh's user avatar
  • 54.7k
25 votes
Accepted

What is ${@:$#} exactly?

In every POSIX compliant shell, $# is the number of arguments to the function or script, the number of positional parameters. $@ is the list of arguments to the function or script, the list $1, $2, ...
Quasímodo's user avatar
  • 19.4k
24 votes

Passing named arguments to shell scripts

I found the solution from cdmo the best, because it is not only limited to a single letter. With a slight adjustment it will consume either whitespace separated arguments like --url www.example.com ...
Jazzschmidt's user avatar
23 votes

Passing named arguments to shell scripts

I just came up with this script while [ $# -gt 0 ]; do if [[ $1 == *"--"* ]]; then v="${1/--/}" declare $v="$2" fi shift done pass it like my_script --p_out /some/path --...
Shahzad Malik's user avatar
23 votes
Accepted

How to give a comma-separated list as arguments to the next command

This should equally work as well: s1 | xargs -d "," -n1 s2 Test case: printf 1,2,3,4 | xargs -d ',' -n1 echo Result: 1 2 3 4 If s1 outputs that list followed by a newline character, you'd want to ...
George Udosen's user avatar
23 votes
Accepted

What does a hyphen do next to the argument position in bash shell script? Like ${1-}

A hyphen in a parameter expansion allows a default value to be specified. So ${1-} means “the value of the first parameter if it is set, and the empty string otherwise”. That doesn’t seem particularly ...
Stephen Kitt's user avatar
21 votes

Passing named arguments to shell scripts

CAVEAT The solution below does not check for incomplete / malformed command invocation! For example, if --p_out requires an argument, but the command is called as my-script.sh --p_out --arg_1 27 then ...
toraritte's user avatar
  • 1,222
21 votes

UNIX-, BSD-, GNU-options in Linux's ps command. Where are they from?

It's all because of a person by the name of Albert D. Cahalan. Xe did not know BSD. The original ps command for Linux was written and first published in March 1992 by Branko Lankester. It was later ...
JdeBP's user avatar
  • 71.9k
21 votes
Accepted

Space not taken as an argument separator by shell script (could someone please explain that small file difference ?)

c2 a0 is the UTF-8 encoding of the non-breaking space character. It usually looks like a regular space, but isn't recognized as whitespace by the shell. In a few keymaps, something like AltGr+Space, ...
ilkkachu's user avatar
  • 148k
20 votes
Accepted

Bash echo $-1 prints hb1. Why?

You are not asking it to print the 1st argument, that would be: $1. What you are asking for is a special parameter: - ($-, a hyphen.) Expands to the current option flags as specified upon ...
jesse_b's user avatar
  • 41.5k
19 votes

Solving "mv: Argument list too long"?

Sometimes it's easiest to just write a little script, e.g. in Python: import glob, shutil for i in glob.glob('*.jpg'): shutil.move(i, 'new_dir/' + i)
duhaime's user avatar
  • 433
19 votes

Why do I need to use cd "$@" instead of cd "$1" when writing a wrapper for cd?

Using "$@" will pass all arguments to cd where as $1 will only pass the first argument. In your examples $ . cdtest.sh "r st" always works as you only pass in one argument, but if you were to pass ...
Nous's user avatar
  • 4,078
19 votes

Is passing a different $0 to a child bash script possible?

You cannot "fool" the child script, but you can call it via a different name by creating a symlink to the called script and calling the symlink. This will, I believe achieve the objective you are ...
John's user avatar
  • 17.4k
18 votes

Multiple arguments in shebang

The POSIX standard is very terse on describing #!: From the rationale section of the documentation of the exec() family of system interfaces: Another way that some historical implementations handle ...
Kusalananda's user avatar
  • 356k
18 votes

unshift args after calling shift 1

You never need to use shift 1 in the first place. Just use the positional arguments and slice around their indices to pass the arguments. first_arg="$1" Once you do this, the rest of the arguments ...
Inian's user avatar
  • 13.1k
18 votes

Why is `sort < "$f1" ` preferred over `sort -- "$f1"`, and why is this preferred over `sort "$f1"`?

The issue is file names beginning with a dash. sort "$f1" doesn't work if the value of f1 starts with - because the command will interpret the value as an option. This usually results in an error but ...
Gilles 'SO- stop being evil''s user avatar
17 votes

Why doesn't using multiple commands with a || or && conditional work?

This: [ "${#}" -eq 1 ] || echo "Invalid number of arguments, expected one."; exit 1 is not: [ "${#}" -eq 1 ] || { echo "Invalid number of arguments, expected one."; exit 1; } But instead is: { [ "${...
muru's user avatar
  • 77.9k

Only top scored, non community-wiki answers of a minimum length are eligible