Questions tagged [function]

Questions on function usage in the context of Unix & Linux (mostly but not exclusively shell scripts). Questions on programming in Python, Perl, Ruby, etc. should be asked on Stack Overflow.

In the context of programming a function is a sub-routine that on input produces output or side effects defined by an algorithm. Functions are used to structure a program and to encapsulate smaller tasks.

Since functions make typically sense only in the context of a specific programming language, the function tag should be used together with a tag indicating the programming language.

565 questions
529
votes
8 answers

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

I know that shell scripts just run commands as if they were executed in at the command prompt. I'd like to be able to run shell scripts as if they were functions... That is, taking an input value or string into the script. How do I approach doing…
Paul
  • 9,163
  • 12
  • 32
  • 30
427
votes
16 answers

In Bash, when to alias, when to script and when to write a function?

Noone should need 10 years for asking this question, like I did. If I were just starting out with Linux, I'd want to know: When to alias, when to script and when to write a function? Where aliases are concerned, I use aliases for very simple…
ixtmixilix
  • 13,040
  • 27
  • 82
  • 118
170
votes
8 answers

Can I "export" functions in bash?

source some_file some_file: doit () { echo doit $1 } export TEST=true If I source some_file the function "doit" and the variable TEST are available on the command line. But running this script: script.sh: #/bin/sh echo $TEST doit test2 Will…
Nils
  • 18,202
  • 11
  • 46
  • 82
140
votes
6 answers

difference between "function foo() {}" and "foo() {}"

I can define bash functions using or omitting the function keyword. Is there any difference? #!/bin/bash function foo() { echo "foo" } bar() { echo "bar" } foo bar Both calls to functions foo and bar succeed and I can't see any difference.…
Carlos Campderrós
  • 2,041
  • 2
  • 16
  • 18
81
votes
13 answers

Why write an entire bash script in functions?

At work, I write bash scripts frequently. My supervisor has suggested that the entire script be broken into functions, similar to the following example: #!/bin/bash # Configure variables declare_variables() { noun=geese count=three } #…
Doktor J
  • 2,622
  • 2
  • 15
  • 18
77
votes
3 answers

Refresh aliases and functions after defining new aliases and functions?

When I define a new alias in .bash_aliases file or a new function in .bashrc file, is there some refresh command to be able immediately use the new aliases or functions without closing the terminal (in my case xfce4-terminal with a few tabs open,…
xralf
  • 16,149
  • 29
  • 101
  • 149
71
votes
4 answers

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

What is the min and max values of the following exit codes in Linux: The exit code returned from a binary executable (for example: a C program). The exit code returned from a bash script (when calling exit). The exit code returned from a function…
user271801
  • 709
  • 1
  • 5
  • 4
64
votes
5 answers

Scope of Local Variables in Shell Functions

After reading 24.2. Local Variables, I thought that declaring a variable var with the keyword local meant that var's value was only accessible within the block of code delimited by the curly braces of a function. However, after running the following…
maddouri
  • 741
  • 1
  • 6
  • 7
49
votes
3 answers

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

So I started using zsh. I like it all right. It seems very cool and slick, and the fact that the current working directory and actual command line are on different lines is nice, but at the same time, I'm noticing that zsh can be a bit slower than…
ixtmixilix
  • 13,040
  • 27
  • 82
  • 118
49
votes
5 answers

What is "declare" in Bash?

After reading ilkkachu's answer to this question I learned on the existence of the declare (with argument -n) shell built in. help declare brings: Set variable values and attributes. Declare variables and give them attributes. If no NAMEs are…
user149572
46
votes
2 answers

How to use defined function with xargs

This is my code #!/bin/bash showword() { echo $1 } echo This is a sample message | xargs -d' ' -t -n1 -P2 showword So I have a function showword which echoes whatever string you pass as a parameter to the function. Then I have xargs trying to…
GMaster
  • 5,992
  • 3
  • 28
  • 32
44
votes
4 answers

Executing user defined function in a find -exec call

I'm on Solaris 10 and I have tested the following with ksh (88), bash (3.00) and zsh (4.2.1). The following code doesn't yield any result: function foo { echo "Hello World" } find somedir -exec foo \; The find does match several files (as…
rahmu
  • 19,673
  • 28
  • 87
  • 128
43
votes
1 answer

Aliases vs functions vs scripts

This site says, "Shell functions are faster [than aliases]. Aliases are looked up after functions and thus resolving is slower. While aliases are easier to understand, shell functions are preferred over aliases for almost every purpose." Given that…
Wolf
  • 3,005
  • 6
  • 27
  • 35
43
votes
11 answers

Executing a Bash Script Function with Sudo

I have a script that does a number of different things, most of which do not require any special privileges. However, one specific section, which I have contained within a function, needs root privileges. I don't wish to require the entire script to…
BryKKan
  • 2,057
  • 2
  • 14
  • 18
36
votes
4 answers

Do functions run as subprocesses in Bash?

In Advanced Bash-Scripting Guide, in example 27-4, 7-th line from the bottom, I've read this: A function runs as a sub-process. I did a test in Bash, and it seems that the above statement is wrong. Searches on this site, Bash Man, and my search…
user147505
1
2 3
37 38