The tag has no wiki summary.

learn more… | top users | synonyms

145
votes
10answers
14k 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 ...
19
votes
4answers
1k 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 ...
18
votes
1answer
2k 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 ...
14
votes
4answers
5k views

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: ...
13
votes
5answers
481 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 ...
10
votes
4answers
3k 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 ...
10
votes
2answers
240 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. ...
8
votes
3answers
413 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 ...
8
votes
1answer
1k views

How to get current buffer's filename in emacs?

One of the main features I miss about Vim, is that it always saves the filename of the current file in the % buffer (more info). That allows launching commands easily like: ;; compile current file :! ...
7
votes
7answers
4k views

Doing simple math on the command line using bash functions: $1 divided by $2 (using bc perhaps)

Sometimes I need to divide one number by another. It would be great if I could just define a bash function for this. So far, I am forced to use expressions like echo 'scale=25;65320/670' | bc but ...
7
votes
3answers
282 views

Bash Function Decorator

In python we can decorate functions with code that is automatically applied and executed against functions. Is there any similar feature in bash? In the script I'm currently working on, I have some ...
7
votes
2answers
739 views

How do I redefine a bash function in terms of old definition?

Is there any way I can redefine a bash function in terms of its old definition? For example I would like to add the following block of code to the preamble of the function command_not_found_handle (), ...
6
votes
3answers
208 views

Display the function body in Bash

I have setup several functions in my .bashrc file. I would like to just display the actual code of the function and not execute it, to quickly refer to something. Is there any way, we could see the ...
6
votes
1answer
55 views

BASH return to main function

I have a BASH script that calls a function, which calls other functions: #!/bin/bash function foo { function bar { # do something } bar } foo How can I return from bar ...
6
votes
1answer
616 views

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, ...
5
votes
2answers
374 views

How to test if command is alias, function or binary?

I have command foo, how can I know if it's binary, a function or alias?
5
votes
2answers
465 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 ...
5
votes
2answers
93 views

Command re-assigned

I found a Bash script snippet earlier with which to echo a string to stderr: echoerr() { echo "$@" 1>&2; } echoerr hello world This remained in my clipboard, and when I wanted to edit a file ...
5
votes
3answers
249 views

Can you pipe to a .bash_profile function?

I received a great function for highlighting files in Apple's finder using the command-line. It's basically a wrapper for osascript. I got it from Mac OS X: How to change the color label of files ...
5
votes
3answers
954 views

How to set an alias on a per-directory basis?

Suppose you have an alias go, but want it to do different things in different directories? In one directory it should run cmd1, but in another directory it should run cmd2 By the way, I have an ...
5
votes
2answers
189 views

How to define a Bash function that can be used by different scripts

I have defined a bash function in my ~/.bashrc file. This allows me to use it in shell terminals. However, it does not seem to exist when I call it from within a script. How can I define a bash ...
5
votes
2answers
356 views

Gawk: Passing arrays to functions

Stuck with GNU awk 3.1.6 and think I've worked around its array bugs but still have what looks like a scope problem in a 600-line awk program. Need to verify understanding of array scope in awk to ...
4
votes
4answers
2k views

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 ...
4
votes
4answers
663 views

How to make functions created in a bash script persist like those in .bashrc?

My .bashrc was getting a little long, so I decided to break it up into smaller files according to topic and then call these files from within .bashrc as so #my long .bashrc file bash .topic1rc bash ...
4
votes
2answers
273 views

Combine two commands in .bash_profile

In my .bash_profile file, I'd like to setup a single command alias that is two commands that execute one after another. The first command takes an argument from the command line and the second is ...
4
votes
3answers
782 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 ...
4
votes
2answers
270 views

Passing a code block as an anon. function

Is it possible to treat a block of commands as an anonymous function? function wrap_this { run_something # Decide to run block or maybe not. run_something else } wrap_this { do_something ...
4
votes
1answer
252 views

View shell function's current definition

So I am editing bashrc constantly, and I have a terminal open with a working function definition, although bashrc has been updated with a wrong function definition. (Because the definition do not ...
4
votes
2answers
825 views

Bash function not working in Zsh

I have been slowly migrating from Bash to Zsh and have got to the point where everything I have moved across is working well, with one exception. I have a couple of functions in my .bashrc that I use ...
4
votes
4answers
962 views

Is it possible to source a file in bash, but skipping specific functions?

Suppose I have bash_functions.sh: function test(){ } function test2(){ } And in my ~/.bashrc I do: source ~/bash_functions.sh Is it possible to, when sourcing it, avoid sourcing a specific ...
4
votes
1answer
146 views

How can I colorize head, tail and less, same as I've done with cat?

I've got 'color cat' working nicely, thanks to others (see How can i colorize cat output including unknown filetypes in b&w?). In my .bashrc: cdc() { for fn in "$@"; do source-highlight ...
4
votes
2answers
268 views

Function caller positional parameters

I need to read and write the positional parameters $@ of a function's caller. The Bash man page says that: A shell function is an object that is called like a simple command and executes a ...
3
votes
3answers
89 views

Is there something like closures for zsh?

I just decided to try zsh (through oh-my-zsh), and am now playing with precmd to emulate a two-line prompt that has right prompts in more than just the last line. So I clone the default theme, and ...
3
votes
1answer
425 views

for loop in bash function

I recently wrote the following bash function: makeaudiobook () { count=1 almbumartist=$2 for f in $1; do preprocess $f > $f-preprocessed text2wave $f-preprocessed -o $f.wav ...
3
votes
3answers
2k 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: $ data1 xxxyyy And I want the corresponding date. I understand ...
3
votes
2answers
5k views

Passing a variable to a bash script when sourcing it in another bash script?

Suppose I have in main.sh: NAME="$HOME" if [ -f "$HOME/install.sh" ] then . "$HOME/install.sh" "$NAME" fi and in install.sh: echo $1 This is supposed to echo /home/user/, but it echoes ...
3
votes
3answers
2k views

How to pass a string parameter on bash function?

I have this code that does work: get_parameter () { echo "$query" | sed -n 's/^.*name=\([^&]*\).*$/\1/p' | sed "s/%20/ /g" } But I want to replace the "name" with the parameter that I pass ...
3
votes
2answers
365 views

Bash: passing braces as arguments to bash function

I love using the following pattern for searching in files: grep --color=auto -iRnHr --include={*.js,*.html,} --exclude-dir={release,dev,} "span" . I'd like, however, to have this one wrapped into a ...
3
votes
3answers
130 views

How to call a shell function

How can I use the integer value returned by a function in shell script that takes some arguments as input? I am using the following code: fun() { echo "hello ${1}" return 1 } a= fun 2 echo ...
3
votes
2answers
86 views

How to show last command with expanding function in bash

I'm using function like this. $ find-grep () { find . -type f -name "$1" -print0 | xargs -0 grep "$2" ; } After I type: $ find-grep *.c foo I want to get expanded last command string. In this ...
3
votes
1answer
199 views

bash: Accessing function call stack in trap function

Working on a bash function call stack trace... Script traps errors and runs a callStack() function. But on trapping, It always shows a call stack for the callStack() function itself instead of the ...
3
votes
2answers
175 views

bash: get array name from parameter to function with saving indexes

I have a function to show index of chosen element. I'm trying to pass a parameter to function to use it as an array name. This works: getIndex() { arrname=$1[@] b=("${!arrname}") index=1; while ...
3
votes
0answers
18 views

Errtrace and local

I am using the following script: #!/bin/bash -Eu trap 'echo Hi' ERR exit_failure() { echo "Hello, World!" return 1 } sub_failure() { res=$(exit_failure) } sub_failure It results in the ...
2
votes
1answer
301 views

Why do Unix-like systems execute a new process when calling a new function?

Why do Unix-like systems execute a new process when calling a function rather than a dynamic library? Creating a new process is costly in terms of performance when compared to calling a dynamic ...
2
votes
2answers
775 views

creating simple command for sudo apt-get install?

I need to run these commands very often: sudo apt-get install <package> sudo apt-get remove <package> Can I make it simple like: install <package> remove <package> I ...
2
votes
4answers
536 views

Scope of variables when calling function from find

In a bash script I define a function that is called from find. The problem is that the scope of variables does not extend to the function. How do I access variables from the function? Here is an ...
2
votes
2answers
234 views

Pass arguments to function exactly as-is

I have the following function: bar() { echo $1:$2; } I am calling this function from another function, foo. foo itself is called as follows: foo "This is" a test I want to get the following ...
2
votes
3answers
254 views

Run nvm (bash function) via sudo

I want to write a init script that should basically run nvm use v0.11.12 && forever start /srv/index.js as the user webconfig. nvm is a shell function that is declared in ...
2
votes
3answers
64 views

how to prevent alias expansion by `eval` to an arbitrary alias, and keep the endless loop protection on a function?

IMPORTANT: do not use eval! (I learned this later..) In a function, eval expands sleep to its alias, so I prevent the endless loop this way: function FUNCexecEcho() { echo "EXEC: $@"; shopt -u ...
2
votes
3answers
758 views

Bash source — select the right function when two sourced files have the same function name?

My bash script sources a script file (call it file2.sh) according to an argument. (It is either sourced or not.) The script file2.sh contains a function "foo" (call it a modified or improved version ...