The tag has no usage guidance.

learn more… | top users | synonyms

241
votes
14answers
31k views

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

It's taken me almost 10 years of Linux usage to ask this question. It was all trial and error and random late-night internet surfing. But people shouldn't need 10 years for this. If I were just ...
16
votes
5answers
825 views

Stateful bash function

I’d like to implement a function in Bash which increases (and returns) a count with every call. Unfortunately this seems non-trivial since I’m invoking the function inside a subshell and it ...
37
votes
5answers
6k views

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 ...
13
votes
4answers
10k views

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 ...
23
votes
1answer
4k views

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 ...
3
votes
3answers
101 views

How to determine callee function name in a script

To make it short, doing something like: -bash$ function tt { echo $0; } -bash$ tt $0 will return -bash, but how to get the function name called, i.e. tt in this example instead?
11
votes
2answers
2k views

How to document my custom bash functions and aliases?

Problem: I have multiple bash functions and aliases. I can't remember all of them off the top of my head, so I usually end up opening my .bash_functions and .bash_aliases files to find what I need. ...
4
votes
4answers
1k views

What are commands to find shell keywords, built in functions and user defined functions?

I was discussing with my friend on how the commands are parsed in the shell, and he told me that bash searches the command in following order List of aliases List of shell keywords List of ...
10
votes
3answers
888 views

Running an executable in PATH with the same name as an existing function

Sometimes I define a function that shadows an executable and tweaks its arguments or output. So the function has the same name as the executable, and I need a way how to run the executable from the ...
3
votes
3answers
75 views

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 ...
0
votes
2answers
104 views

How to increment local variable in Bash?

Data 1 \begin{document} 3 Code #!/bin/bash function getStart { local START="$(awk '/begin\{document\}/{ print NR; exit }' data.tex)" echo $START } START2=$(getStart) echo ...
11
votes
3answers
15k views

How to add a function to .bash_profile/.profile/bashrc in shell?

I have a function which converts epoch time to date. Here is the definition date1(){ date -d @$1 } I'd like to be able to write: $ date1 xxxyyy Where xxxyyy is the parameter I pass into my ...
4
votes
3answers
2k views

Infinitely Nested Directories

Let me start off by saying this is a Mac Terminal I'm using. Not Linux, but I assumed I would get the best answers here as it has to do with Unix and the command line not really anything about Mac ...
3
votes
4answers
3k views

how can i use sudo within a function?

i have written a function which acts in a similar way to tee but also pre-pends a datestamp. everything works fine except when i want to output to a file which is only root writeable (in my case a ...
1
vote
1answer
90 views

Bash FUNCNAME equivalent in Dash

Bash can print the current function name: $ bash -c 'g(){ echo $FUNCNAME; }; g' g However Dash cannot use FUNCNAME: $ dash -c 'g(){ echo $FUNCNAME; }; g' It is possible to access the current ...
7
votes
1answer
992 views

How does VARIABLE=() { function definition } work in bash

I have seen something like this in my bash ENV: module=() { eval `/usr/bin/modulecmd bash $*` } How does this construct work? What is it called? I'm not asking about modulecmd, I am asking about ...
6
votes
2answers
1k views

How can I create a function in zsh that calls an existing command with the same name?

How can I write a function in zsh that invokes an existing command with the same name as the function itself? For example, I've tried this to illustrate my question: function ls { ls -l $1 $2 $3 ...
3
votes
2answers
167 views

How do I remove all but the file name (with no extension) from a full file path? [duplicate]

I am presently writing a Bash function to convert all the man pages listed by equery files <PACKAGE> | grep /usr/share/man/man (if you are unfamiliar equery is a tool used on Gentoo-based ...
1
vote
1answer
24 views

Which shells have functions where “local” does not alter exported variables for child processes?

In the example below, an exported variable is re-set as local in a function. Bash, Zsh, Fish don't pass on the original value to the child process. Are there any shells that make local affect the ...
1
vote
2answers
157 views

Bash: How to create an alias in .bashrc for awk with parameters

I'm trying to add an alias in .bashrc file as follows: ... alias cutf="_cutf" ... _cutf() { awk 'NR >= $2 && NR <= $3 { print }' < $1 } (The function's goal is to show the ...
1
vote
3answers
3k views

What return/exit values can I use in bash functions/scripts?

I want to know what return values we can use that will not be mistaken by for ex. SIGINT? ex.: $sleep 10 $#hit ctrl+c $echo $? 130 so I know I must not use anything like return 130 or exit 130 so ...
0
votes
2answers
74 views

Its possible use special characters in a shell function name?

I have to create the function in shell script, that function name must contain the special characters. like >() { echo $1 $2 } Here my function name is >, If its ...