Questions tagged [bash-function]

A sequence of bash commands that can be called with a function name.

A sequence of bash commands that can be called with a function name. Functions are defined like this:

function fname() {
  echo Hello world
}

Or:

fname() {
  echo Hello world
}

You can now call:

fname

The same with parameters:

function fname() {
  echo $1 $2
}

Call as:

fname Hello world
38 questions
68
votes
4 answers

read stdin in function in bash script

I have some set of bash functions which output some information: find-modelname-in-epson-ppds find-modelname-in-samsung-ppds find-modelname-in-hp-ppds etc ... I've been writing functions which read output and filter it: function filter-epson { …
likern
  • 3,744
  • 5
  • 36
  • 47
18
votes
7 answers

How can I create a stopwatch in bash?

I created a simple stopwatch (bash function) for counting time, but for now it's showing current time with milliseconds. The code: function stopwatch() { date +%H:%M:%S:%N while true; do echo -ne "`date +%H:%M:%S:%N`\r"; done; } I tried to…
lewiatan
  • 1,126
  • 2
  • 21
  • 37
12
votes
2 answers

Bash function argument returns error "command not found"

I have this function in a bash script, to create a new jekyll post; but it returns the argument as command not found. Here's the script: function new_post () { if [ -z "$1" ] then read -p "Post Title:" TITLE else …
JKirchartz
  • 17,612
  • 7
  • 60
  • 88
10
votes
3 answers

return value from subshell and output to local variables

I've found the strange behaviour for me, which I can't explain. The following code is work OK: function prepare-archive { blah-blah-blah... _SPEC_FILE=$(check-spec-file "$_GIT_DIR/packaging/") exit $? blah-blah-blah... } means I get value which I…
Mephi_stofel
  • 355
  • 2
  • 6
  • 15
9
votes
2 answers

Defining bash function body using parenthesis instead of braces

This script demonstrates defining a bash function with parenthesis verses with braces. The parenthesis have the nice effect of making environment variables created in the function "local", I guess because the function body is executed as a…
ELO
  • 319
  • 1
  • 3
  • 4
8
votes
1 answer

Timing bash functions

I'd like to see how long it takes for a bash function to run. After doing a little research, I've come up with this approach which uses a sub-shell: function test-function() { time ( rsync -av ~/tmp/test-dir-start/ ~/tmp/test-dir-end/ …
Alan W. Smith
  • 24,647
  • 4
  • 70
  • 96
7
votes
2 answers

unset bash function variable with non-standard name

I may have this function in a bash script that gets sourced to the shell function suman{ echo "using suman function" } if I call unset suman things seem to work as expected however if I have this as my function: function suman-inspect { …
Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
5
votes
2 answers

How to override pushd's and popd's automatic call to dirs?

My bash (4.1) directory stack usually has a dozen or more entries. I wanted to replace the output of dirs with dirs -v, so I would never have to play "guess the magic number" with pushd again. My Partial Solution I replaced dirs with a function…
Kevin J. Chase
  • 3,856
  • 4
  • 21
  • 43
5
votes
4 answers

Return a single value from a shell script function

Example: #!/bin/sh a() { R=f ls -1 a* [ "$?" == "1" ] && { R=t; } echo $R } r=`a` echo $r $r contains t or f but also the output of the ls command. I may write ls -1 a* >/dev/null 2>/dev/null, but if there is a more complex script that can be…
PeterMmm
  • 24,152
  • 13
  • 73
  • 111
4
votes
2 answers

BASH: if statement execute command and function

I've run into an issue in which I think should be easy to resolve, but for the life of me, I can't figure it out. Could be that it's really late; not sure. So I have a shell script and I have an if statement that I need to run. The problem is that…
drewrockshard
  • 2,043
  • 10
  • 35
  • 47
4
votes
1 answer

How to launch a Bash function using Git alias

I want to use a Git alias in ~/.gitconfig so that it calls a bash function, if it is defined, otherwise call the regular git checkout. This is what I have devised: cat ~/.gitconfig ... [alias] ... co = !(compgen -A function vxzExecuteGitCheckout…
Gurjeet Singh
  • 2,635
  • 2
  • 27
  • 22
4
votes
3 answers

Bash function to process all dotfiles in a directory excluding directories

I am looking to create a bash function to filter all the dotfiles (no directories) in a selected directory. I only need the file name, not the full path. For the moment, i only have this command: find . -maxdepth 1 -type f -print0 which prints all…
Jeanmichel Cote
  • 531
  • 1
  • 5
  • 19
4
votes
2 answers

Passing alias as function argument linux bash

Hi everyone I'm learning how to use the .bashrc file in linux and as my title states I'm wondering how to make a function recognize an argument as an alias I have an alias called home defined as: alias home=$HOME and a function go defined…
Alex
  • 1,388
  • 1
  • 10
  • 19
3
votes
3 answers

Spawn a background process in a bash function

I am working on writing a Bash function to start a server that needs to be started from a certain folder, but I don't want starting this server to impact my current work. I've written the following: function startsrv { pushd . cd…
foxxtrot
  • 11,214
  • 4
  • 27
  • 27
3
votes
1 answer

In bash tee is making function variables local, how do I escape this?

I have stucked with a bash scipt which should write both to stdout and into file. I'm using functions and some variables inside them. Whenever I try to redirect the function to a file and print on the screen with tee I can't use the variables that I…
LinenG
  • 109
  • 3
  • 14
1
2 3