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 "\...
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 ...
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 ...
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, ...
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 ......
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 ...
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,...
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 ...
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"...
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. ...
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 ...
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 ...
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
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 ...
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 ...
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
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 ...
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 ...
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.
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() {
...
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. ...
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
...
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 ...
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-...
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 ...
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 ...
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 ...
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 \$*)}; ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
function × 582bash × 379
shell-script × 145
shell × 116
zsh × 66
alias × 53
variable × 41
arguments × 36
scripting × 34
environment-variables × 20
array × 18
linux × 17
parameter × 15
pipe × 14
ksh × 14
find × 12
sudo × 11
exit-status × 11
ssh × 10
awk × 10
command-line × 10
command × 10
quoting × 9
for × 8
vim × 7