Propagate all arguments following nth argument in a zsh script

shell script arguments
bash call script with all arguments
bash all arguments except last
bash all arguments except first
bash script arguments
pass arguments to shell script
bash get all arguments after the first
bash positional parameters

If I have a script like

curl --socks5-hostname "${2-1080}" $1 

I can do:

purl 1081


curl --socks5-hostname ""

Now I want to dynamically add new arguments like:

purl 1081 --connect-timeout 1

How would I be able to do it?

If I use

curl --socks5-hostname "${2-1080}" $1 "$@"

Then it would end up as:

curl --socks5-hostname "" 1081 --connect-timeout 1

This is not the desirable outcome...

I would like:

curl --socks5-hostname "" --connect-timeout 1

You can use shift to "consume" the host and optional port, so that the remaining arguments can be passed on to curl.

host=${1:-Missing host}  # Exits if *no* arguments available.

if [[ $1 =~ ^[0-9]+ ]]; then

curl --sock5-hostname "$port" "$host" "$@"

$@ except the 1st argument, After shift , what used to be $1 has been removed from the list (but The positional parameters have been shifted 1 position to the left But note that it's not standard sh syntax so should not be used in a script that starts with #! /bin/sh - . simply loops over every argument, but doesn't process the first one. Arguments are set of characters between spaces added after the script. To specify an argument that includes spaces, you need to enclose the complete argument in double quotation marks. Each script argument entered at the command line has a default variable name. The first argument after the script name is assigned to the variable $1, the second


curl --socks5-hostname "${2-1080}" $1 "{@:3}"


shift 2
curl --socks5-hostname "$port" $host "$@"

command line - Passing arguments to a script, A dollar sign followed by a number N corresponds to the Nth argument passed to the script. As for $? , consider the following script snippet: That's what happens when you attempt to expand a shell parameter that is not That's probably what you'd expect, since the script doesn't refer to them at all. If you choose this, pass each string (the program and its argument) as a separate argument to your script. These would typically be the last arguments to your script (if you want to be able to pass more arguments, you need to designate a special string as an end-of-program-arguments marker, which you then can't pass to the program unless you

you can change your script into:

curl --socks5-hostname "${p[1]-1080}" ${p[0]} "${p[@]:2}"

Writing shell scripts, All of these features involve using command line options and arguments. If some_program were a bash shell script, we could read each item on the command line because the positional parameters contain the following: then echo "Positional parameter 1 contains something" else echo "Positional parameter 1 is empty"� Sometimes you want to pass all your arguments, but preceded by a flag (e.g. --flag) $ bar --flag "$1" --flag "$2" --flag "$3" You can do this in the following way: $ bar $(printf -- ' --flag "%s"' "$@") note: to avoid extra field splitting, you must quote %s and $@, and to avoid having a single string, you cannot quote the subshell of printf.

Shell Command Language, The rule in Parameter Expansion shall be used to determine the matching '}'. followed by all of the characters preceding the first unquoted <slash> in the Any valid shell script can be used for command, except a script consisting If n is specified, the break utility shall exit from the nth enclosing for, while, or until loop. I'm trying to write a script notify-finish that can be prepended to any command. When done, it will run the command given by the arguments following, then email the user when the command is complete.

Man page of ZSHALL, This manual page includes all the separate manual pages in the following order: In sh and ksh compatibility modes the following parameters are not special and not initialized by the shell: ARGC, argv, The usual zsh startup/shutdown scripts are not executed. 0: The first input word (command). n: The nth argument. If zsh was invoked to run a script, this is the name of the script. Otherwise, it is the name used to invoke the current shell. This is the same as the value of $0 when the POSIX_ARGZERO option is set, but is always available.

zshall(1), This manual page includes all the separate man- ual pages in the following order : In sh and ksh compatibility modes the following parameters are not spe- cial and not initialized The usual zsh startup/shutdown scripts are not executed. Word designators include: 0 The first input word (command). n The nth argument. $* or $@ holds all parameters or arguments passed to the function. $# holds the number of positional parameters passed to the function. How Do I Display Function Name? $0 always point to the shell script name. However, you can use an array variable called FUNCNAME which