Skip to main content
147 votes

How can I pass a command line argument into a shell script?

On a bash script, I personally like to use the following script to set parameters: #!/bin/bash helpFunction() { echo "" echo "Usage: $0 -a parameterA -b parameterB -c parameterC" echo -e "\...
Rafael Muynarsk's user avatar
110 votes

What is the min and max values of exit codes in Linux?

The number passed to the _exit()/exit_group() system call (sometimes referred as the exit code to avoid the ambiguity with exit status which is also referring to an encoding of either the exit code or ...
Stéphane Chazelas's user avatar
79 votes
Accepted

call function declared below

Like others have said, you can't do that. But if you want to arrange the code into one file so that the main program is at the top of the file, and other functions are defined below, you can do it by ...
ilkkachu's user avatar
  • 141k
61 votes
Accepted

Why write an entire bash script in functions?

I've started using this same style of bash programming after reading Kfir Lavi's blog post "Defensive Bash Programming". He gives quite a few good reasons, but personally I find these the ...
Sergiy Kolodyazhnyy'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.1k
54 votes
Accepted

Is the "callback" concept of programming existent in Bash?

In typical imperative programming, you write sequences of instructions and they are executed one after the other, with explicit control flow. For example: if [ -f file1 ]; then # If file1 exists ......
Stephen Kitt's user avatar
52 votes

Scope of Local Variables in Shell Functions

Shell variables have a dynamic scope. If a variable is declared as local to a function, that scope remains in effect until the function returns, including during calls to other functions! This is in ...
Gilles 'SO- stop being evil''s user avatar
52 votes
Accepted

What is "declare" in Bash?

In most cases it is enough with an implicit declaration in bash asdf="some text" But, sometimes you want a variable's value to only be integer (so in case it would later change, even automatically, ...
sudodus's user avatar
  • 6,461
47 votes
Accepted

Do functions run as subprocesses in Bash?

The Advanced Bash-Scripting Guide is not always reliable and its example scripts contain out-dated practices such as using the effectively deprecated backticks for command substitution, i.e., `command`...
Anthony Geoghegan's user avatar
45 votes

How can I pass a command line argument into a shell script?

The form $ ./script.sh "$@" is the most convenient for argv. The arg delimiter is whitespace, but can be changed. Don't remember how off-hand. Then use $1, $2, shift, if [ $# -ne 3 ] to control ...
user400344's user avatar
45 votes

Do functions run as subprocesses in Bash?

Curly brace functions will run within the calling shell process, unless they need their own subshell which is: when you run them in the background with & when you run them as a link in a ...
Petr Skocik's user avatar
  • 29.1k
43 votes
Accepted

Alias and functions

Aliases are expanded when a function definition is read, not when the function is executed … $ echo "The quick brown fox jumps over the lazy dog." > myfile   $ alias myalias=cat   $ myfunc() { >...
G-Man Says 'Reinstate Monica''s user avatar
43 votes

Why is $1 in a function not printing the script's first argument?

Positional parameters refer to the script's arguments in the main level of the script, but to function arguments in function body. So print_something Something would actually print Something. If ...
weirdan's user avatar
  • 551
40 votes

Why write an entire bash script in functions?

In my comment, I mentioned three advantages of functions: They are easier to test and verify correctness. Functions can be easily reused (sourced) in future scripts Your boss likes them. And, never ...
John1024's user avatar
  • 75.2k
35 votes
Accepted

How to make a multiline alias in Bash?

It's not impossible at all. alias thing='( cd "${program_to_update_dir}" wget "https://raw.githubusercontent.com/USER/PROJECT/BRANCH/update.sh" source update.sh rm update.sh )' or,...
Kusalananda's user avatar
  • 339k
31 votes
Accepted

Bash function that accepts input from parameter or pipe

See Stéphane Chazelas's answer for a better solution. You can use /dev/stdin to read from standard input b64decode() { if (( $# == 0 )) ; then base64 --decode < /dev/stdin echo ...
Sundeep's user avatar
  • 12.1k
31 votes
Accepted

How to pass all arguments of a function along to another command?

Within your program shell function, use "$@" to refer to the list of all command line arguments given to the function. With the quotes, each command line argument given to program would ...
Kusalananda's user avatar
  • 339k
30 votes
Accepted

Test for function's existence that can work on both bash and zsh?

If you want to check that there's a currently defined (or at least potentially marked for autoloading) function by the name foo regardless of whether a builtin/executable/keyword/alias may also be ...
Stéphane Chazelas's user avatar
30 votes
Accepted

Exporting a variable from inside a function equals to global export of that variable?

Your script creates an environment variable, myVar, in the environment of the script. The script, as it is currently presented, is functionally exactly equivalent to #!/bin/bash export myVar="myVal"...
Kusalananda's user avatar
  • 339k
28 votes
Accepted

How to pass parameters to function in a bash script?

To call a function with arguments: function_name "$arg1" "$arg2" The function refers to passed arguments by their position (not by name), that is $1, $2, and so forth. $0 is the name of the script ...
Rahul's user avatar
  • 13.7k
27 votes

Is the "callback" concept of programming existent in Bash?

First it's important to note that what makes a function a callback function is how it's used, not what it does. A callback is when code that you write is called from code that you didn't write. You're ...
Gilles 'SO- stop being evil''s user avatar
25 votes
Accepted

How to make custom zsh script executable automatically?

You're mixing up scripts and functions. Making a script A script is a standalone program. It may happen to be written in zsh, but you can invoke it from anywhere, not just from a zsh command line. ...
Gilles 'SO- stop being evil''s user avatar
24 votes
Accepted

Can you explain these three things in this bash code for me?

d=$d/.. adds /.. to the current contents of the d variable. d starts off empty, then the first iteration makes it /.., the second /../.. etc. sed 's/^\///' drops the first /, so /../.. becomes ../.. (...
Stephen Kitt's user avatar
23 votes

What is "declare" in Bash?

The output of help declare is quite terse. A clearer explanation can be be found in man bash or info bash — the latter being the source for what follows. First, some definitions. About variables and ...
fra-san's user avatar
  • 10.3k
22 votes

call function declared below

No, the functions have to exist in the shells environment at the time of calling them. Google's "Shell Style Guide" has a fix for this: A function called main is required for scripts long enough ...
Kusalananda's user avatar
  • 339k
22 votes
Accepted

function's calling context in zsh: equivalent of bash `caller`

I don't think there's a builtin command equivalent, but some combination of these four variables from the zsh/Parameter module can be used: funcfiletrace This array contains the absolute line ...
muru's user avatar
  • 73.9k
21 votes

What is "declare" in Bash?

I’ll have my go at trying and explain this, but forgive me if I won’t follow the example you provided. I’ll rather try to guide you along my own, different, approach. You say you already understand ...
LL3's user avatar
  • 5,458
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 ...
Michael Daffin's user avatar
19 votes

How to undefine a zsh command created accidentally?

This defines two functions, one named grep and the other named vars, whose body is *.py: grep .vars() *.py To remove those functions --- and ...
Andy Dalton's user avatar
  • 14.2k

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