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
3
votes
2 answers

Use a subrange of all arguments in a Bash function

I have this function in a Bash script: comp() { rsync -v --archive $1/ $TMP/$2 $3 $4 $5 $6 $7 $8 $9 } As you can see, I'm doing something special with arguments $1 and $2. Then I hackily just append all the rest of them to the end of the…
Bart van Heukelom
  • 43,244
  • 59
  • 186
  • 301
3
votes
2 answers

Bash function fails when it's part of a pipe

This is an attempt to simplify a problem I'm having. I define a function which sets a variable and this works in this scenario: $ function myfunc { res="ABC" ; } $ res="XYZ" $ myfunc $ echo $res ABC So res has been changed by the call to…
Thorsen
  • 403
  • 1
  • 4
  • 10
2
votes
4 answers

bash scripting case switch

I am trying to initialize a variable in a case statement in a bash script, function cpfiles(){ case $1 in a) echo "a" ; source = ${HOME}/dev/a.zip ; dest = 'PROGRA~2\\a.zip';; b) echo "b" ; source = ${HOME}/dev/b.zip ; dest =…
Michael Biniashvili
  • 500
  • 1
  • 12
  • 24
2
votes
3 answers

how to pipe output from find to pdf viewer

Hello Bash Superheros, I am trying to create a simple bash function that pipes the output from find to a a pdf viewer, similar but limited to evince. function findpapers { find ~/papers/ -name *$1* | evince } The above function opens the…
Alex Witsil
  • 855
  • 8
  • 22
2
votes
3 answers

accessing script parameter from bash function without passing them on call

Is there any way to access bash script parameters from script function if the function is not explicitly called with script parameters? Thanks.
jackhab
  • 17,128
  • 37
  • 99
  • 136
1
vote
1 answer

bash aliases not recognized by a bash function: sunspot_rails, jruby, rspec

Aliases below for running sunspot in the background work Aliases below for finding and killing those instances work ENV variables for the sunspot ports are accessible But, Functions for running sunspot, processing command, and killing sunspot only…
C Del
  • 53
  • 6
1
vote
2 answers

How can I check if my Bash function got an argument or not?

Quick question. I love emacs. I hate typing stuff though, so I like to use e to invoke emacs. Previously I had my .bash_profile (OS X) configured as: alias e="emacs .". However, I was tired of still having to type emacs {file} when I just wanted to…
Josh
  • 12,448
  • 10
  • 74
  • 118
1
vote
2 answers

How do I send a command with a message to echo on success to a bash function?

I've written a function to check if a command executed successfully, and if not, throw an error. assert_success () { "$1" if [[ $? == 0 ]]; then echo "$2" fi } Say I have the following command to execute with the given error…
gran_profaci
  • 8,087
  • 15
  • 66
  • 99
1
vote
1 answer

bash function ignoring dot for git clone command

I am wrestling with a bash function that will not clone to current directory, it is making a project folder: cloneproject() { git clone git@bitbucket.org:codyc54321/$1.git . ;} I have the dot at the end, before semicolon, but running this within a…
codyc4321
  • 9,014
  • 22
  • 92
  • 165
1
vote
1 answer

Bash function with formatting

I'm using the following in my .bashrc file as a function to grep info from an external LDAP but would love for it to output a couple of values, but each on their own line function ldaps() { ldapsearch -x -H ldaps://ldap-server.example.com -b…
Dan
  • 931
  • 2
  • 18
  • 31
1
vote
2 answers

trap in exported function silently ignored

This Bash script behaves as expected. test_this.sh function run_this() { trap "echo TRAPPED" EXIT false echo $? } run_this It prints 1 TRAPPED However, when I try to export this function, it fails to trap. test_this2.sh function…
ishmael
  • 1,796
  • 3
  • 18
  • 19
1
vote
1 answer

.bashrc function implementing grepall

I have 2 bash functions catall and grepall catall works fine, catting every file it finds with the file name printed first, then the content and a new line catall () { find . -name $1 | xargs -I % sh -c 'echo %; cat %; echo"" ' } grepall…
dmc
  • 401
  • 4
  • 14
1
vote
1 answer

accesing functions from external files bash

This is really simple but since i m still a newbiw in shell scripting, i not able to do this. It is simple, i have a menu driven script in which when the user enters his choice i loop in the case function and execute the selected menu. Uptil now i…
Sam007
  • 8,397
  • 4
  • 24
  • 34
0
votes
2 answers

How to get and compare Bash Function parameters?

this should be a simple answer but I haven't been able to find a simple solution. I'm trying to create a bash script that runs the reindent.py tool scrip but I want to be able to get user input on whether or not he wants to write the file back to…
Alexandre Krabbe
  • 727
  • 1
  • 13
  • 33
0
votes
1 answer

Bash predicate with several boolean functions

Writing bash script and don't know how to use more that one function in predicate: #!/bin/bash set -x WAITED=0 registered () { VBoxManage showvminfo --machinereadable "$1" 2>/dev/null | grep UUID= } not_running () { VBoxManage showvminfo…
Jonas
  • 4,683
  • 4
  • 45
  • 81