Skip to main content
151 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
113 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
84 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
  • 148k
61 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,686
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
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
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 you ...
weirdan's user avatar
  • 551
38 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
  • 356k
36 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
  • 356k
34 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
  • 356k
27 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
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
27 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.8k
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

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 to ...
Kusalananda's user avatar
  • 356k
23 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,573
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
  • 77.8k
21 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.7k
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
18 votes

what is the zsh equivalent of bash's export -f

If you put your function declaration in .zshenv, your function will be usable from a script without any effort.
rools's user avatar
  • 516
16 votes
Accepted

Is there a way to get the positional parameters of the script from inside a function in bash?

No, not directly, since the function parameters mask them. But in Bash or ksh, you could just assign the script's arguments to a separate array, and use that. #!/bin/bash ARGV=("$@") foo() { ...
ilkkachu's user avatar
  • 148k
16 votes
Accepted

Shell: Using function with parameters in if

When using if [ ... ] you are actually using the [ utility (which is the same as test but requires that the last argument is ]). [ does not understand to run your function, it expects strings. ...
Kusalananda's user avatar
  • 356k
15 votes

Executing a Bash Script Function with Sudo

I've written my own Sudo bash function to do that, it works to call functions and aliases : function Sudo { local firstArg=$1 if [ $(type -t $firstArg) = function ] then ...
SebMa's user avatar
  • 2,453
15 votes

show only physical disks when using df and mount

Reading man mount Listing the mounts The listing mode is maintained for backward compatibility only. For more robust and customizable output use findmnt(8), especially in your scripts. showed ...
guntbert's user avatar
  • 1,717
15 votes
Accepted

overwrite and reuse existing function in zsh

How to patch a function The code of a function is stored in the associative array functions. That's the source code with normalized whitespace and no comments (zsh has done lexical analysis and pretty-...
Gilles 'SO- stop being evil''s user avatar
14 votes

Scope of Local Variables in Shell Functions

In function innerFunc() the var='new value' wasn't declared as local, therefore it's available in visible scope (once the function has been called). Conversely, in function outerFunc() the local ...
Joseph Tingiris's user avatar
14 votes

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

There's also the case when there are no arguments: $ cd /tmp; cd; pwd /home/muru $ cd_func() { builtin cd "$1"; } $ cd_func /tmp; cd_func; pwd /tmp cd without any arguments changes to the home ...
muru's user avatar
  • 77.8k
14 votes
Accepted

Strange variable scope behavior when calling function recursivly

Do I get it right that you want to count all files in a directory tree, or something like that? I.e. you want the variable count to be global? Your issue is here: find $1 | while read line; do See ...
ilkkachu's user avatar
  • 148k
13 votes
Accepted

How can a bash function return multiple values?

Yes, bash's return can only return numbers, and only integers between 0 and 255. For a shell that can return anything (lists of things), you can look at es: $ es -c "fn f {return (a 'b c' d \$*)}; ...
Stéphane Chazelas's user avatar

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