Questions tagged [bash-functions]
69 questions
23
votes
3 answers
Implicit return in bash functions?
Say I have a bash function like so:
gmx(){
echo "foo";
}
will this function implicitly return the exit value of the echo command, or is using return necessary?
gmx(){
echo "foo";
return $?
}
I assume that the way bash works, the exit status…
Alexander Mills
- 9,330
- 19
- 95
- 180
11
votes
2 answers
`set -e` inside a bash function
Does set -e behave differently here
set -e;
function foo {
}
vs.
function foo {
set -e;
}
does set -e belong inside functions? Does set -e declared outside of functions, affect "nested" functions inside a shell file? What about the inverse?…
Alexander Mills
- 9,330
- 19
- 95
- 180
10
votes
3 answers
Why is sh (not bash) complaining about functions defined in my .bashrc?
I am getting this one when I open a terminal session:
sh: error importing function definition for `read.json'
sh: error importing function definition for `ts-project'
sh doesn't like these functions because they look like:
read.json(){
…
Alexander Mills
- 9,330
- 19
- 95
- 180
7
votes
1 answer
Bash return an associative array from a function and then pass that associative array to other functions
I'm trying to build an associative array in a function from a list pass in via an arg, but It just isn't working:
#!/usr/bin/env bash
function cwd {
echo "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
}
function list_commands {
local…
Thermatix
- 351
- 4
- 14
7
votes
6 answers
How to export all Bash functions in a file in one line?
In Ubuntu 16.04 I have a Bash file containing a few different functions for automating various common tasks on my system.
I have sourced that file in bashrc so I could comfortably call each function from anywhere in the terminal in time of need,…
Arcticooling
- 1
- 12
- 44
- 103
6
votes
1 answer
Why does declare -F give the wrong file
I have 2 small functions to abbreviate set -x / set +x, namely:
sx () {
export PS4='+ [${BASH_SOURCE##*/}:${LINENO}]'
set -x
}
and
sz () {
set +x
export PS4=
}
These 2 functions are in a file functons.sh which is sourced from…
jalanb
- 578
- 4
- 15
5
votes
1 answer
Start function in background and save PID
I have a script containing two functions. one function starts the other as a background process and returns that processes UID. This works well, as long as I don't want to put the PID into a dedicated variable. Once I try to assign the PID to a…
Koltak
- 53
- 1
- 4
3
votes
1 answer
define and export a function in without repeating yourself
I know I can do this:
function foo() { echo "foo"; }; export -f foo
But can I do this without repeating the function name?
Alan Berezin
- 133
- 3
3
votes
1 answer
Failing on errors inside a function which is called on the left-hand-side of an && expression
I am working on a Bash script with several functions. I would like to exit the script if any commands return an nonzero exit code, unless in a context where that code is being explicitly handled (such as in an if condition or before a ||…
Jeremy
- 1
- 1
- 4
- 13
3
votes
0 answers
Git - prune every whitespace-separated word originally introduced by specific author in project's history
We have a project under git revision control with only a single branch. We need to remove every new whitespace-separated word that was introduced for the first ever time in a given file by a specific author.
To clarify, at this point we have the…
Dr Krishnakumar Gopalakrishnan
- 395
- 2
- 14
2
votes
2 answers
How to squelch sh errors from imported bash functions?
So I've got a .bash_aliases file with many exported bash functions inside; a good deal of which are incompatible with sh...
The problem is that sh is used all over my laptop (OS X) and linux machines that I ssh into. In addition it always tries to…
profPlum
- 125
- 5
2
votes
1 answer
MacOS: Changing screen capture location
I make a lot of presentations that involve many screenshots, and I want an easier way to organize them by project. I'm trying to write a simple function that changes the location where screenshots are saved to the current working directory.
I've…
condenastee
- 23
- 2
2
votes
1 answer
How may I echo all but the last parameter in bash?
I have the following
#!/bin/bash
function f1 ()
{
echo "${@:1:-2}"
}
f1 1 2 3 4 5 6
I need to echo 1 2 3 4 5
man bash tells me that when I use @ I can't use a negative length.
I resorted to using a calculating ("${@:1:$((${#@}-1))}") which is…
Bret Joseph
- 335
- 1
- 3
- 12
2
votes
3 answers
grep with heredoc in function
I want to make function that parses from text pasted in a terminal.
$ cat < foo
> sometext
> sometext1
> a sometext
> asdf
>
> eof
sometext
sometext1
a sometext
While the above works I can make neither an alias nor a…
1.61803
- 1,201
- 2
- 15
- 23
2
votes
1 answer
Return last command executed in bash function
Similar to this: Return last command executed in shell-script.
lastCommand=$(some command here)
Is it possible to achieve same result, but instead used inside a bash function,
not a bash script?
P.S.
lastCommand mean previous command inside the…
qeatzy
- 226
- 1
- 6