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 "\...
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 ...
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 ...
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 ...
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 ...
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 ......
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 ...
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, ...
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`...
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 ...
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 ...
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() {
>...
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 ...
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 ...
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,...
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
...
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 ...
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 ...
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"...
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 ...
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 ...
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. ...
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 ../.. (...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
function × 575bash × 373
shell-script × 142
shell × 112
zsh × 65
alias × 51
variable × 41
arguments × 36
scripting × 33
environment-variables × 19
array × 18
linux × 17
parameter × 15
pipe × 14
ksh × 14
find × 12
sudo × 11
exit-status × 11
ssh × 10
command-line × 10
command × 10
awk × 9
quoting × 9
for × 8
vim × 7