2
votes
2answers
106 views

How to force watch to run under bash

The problem is that when watch is executed it runs sh and I get this error: sh: 1: func1: not found here is the code: #!/bin/bash func1(){ echo $1 } export -f func1 watch func1
6
votes
1answer
52 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 ...
0
votes
2answers
72 views

Exit the bash function, not the terminal

I have a bash script as below which installs zookeeper but only if not installed already. ##zookeper installZook(){ ZOOK_VERSION="3.4.5" ZOOK_TOOL="zookeeper-${ZOOK_VERSION}" ...
2
votes
1answer
58 views

Value assigned inside a function variable is alway empty

I have a following simple script. In this script, I am assigning value to a global variable inside a function. I can clearly see that the value being assigned to the variable via debug statement. ...
1
vote
1answer
21 views

Bash array declared in a function is not available outside the function

on bash (v4.3.11) terminal type this: function FUNCtst() { declare -A astr; astr=([a]="1k" [b]="2k" ); declare -p astr; };FUNCtst;declare -p astr (same thing below, just to be easier to read here) ...
10
votes
2answers
226 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. ...
0
votes
1answer
44 views

apply bash function to parent scope

I can to define function in bash and use it: foo() { echo $1; } foo test But if I want to collect my functions in one bash script all its unavailable: init.bash #!/bin/bash foo() { echo $1; } ...
7
votes
3answers
275 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 ...
2
votes
3answers
241 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 ...
13
votes
5answers
470 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 ...
1
vote
2answers
20 views

Install-on-Demand Wrapper Function for Executables

How do I make the following function work correctly # Install git on demand function git() { if ! type git &> /dev/null; then sudo $APT install git; fi git $*; } by making git $* ...
2
votes
3answers
59 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 ...
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 ...
1
vote
1answer
297 views

How to export a function in bash over ssh?

I would like to write a function in bash, then export that function and execute it over ssh. Is that possible, and if yes, how? I tried #!/bin/bash function myfunc() { echo $1 } export ...
1
vote
1answer
186 views

getopts - how to avoid read next $OPTARG as argument?

Just started use getopts and found one problem... For example - have a script with: while getopts "h:loav" opt; do case $opt in h) h=$OPTARG echo $h ;; But whet I run it: $ ./ftpclean.sh -h ...
0
votes
1answer
150 views

Why does a working standalone nested function/script not work inside a larger script? [duplicate]

The following (nested) function/s function hpf_matrix { # Positional Parameters Matrix_Dimension="${1}" Center_Cell_Value="${2}" # Define the cell value(s) function hpf_cell_value { ...
3
votes
2answers
168 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 ...
0
votes
2answers
85 views

Why must you be careful when using Bash's built in command history function to re-run previous commands that contain variables?

I know !! re-runs commands but what exactly would occur if I re-ran a command that had a variable in the command?
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 ...
1
vote
1answer
153 views

Execute command supplied by function parameters

I'm trying to create a function method in a bash script that executes a command which is supplied to the method by the paramters. Meaning somethings like this: special_execute() { # Some code ...
2
votes
4answers
500 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 ...
1
vote
2answers
629 views

Bash function to scp a file not working

I am new to bash scripting and read basic tutorials online and wrote following simple bash function: function to_company() { scp ${1} username@long.server.name.company.com://home/username } ...
2
votes
3answers
727 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 ...
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 ...
1
vote
2answers
3k views

How to return the exit code? Error: return: Reading: numeric argument required

Here's a simplified version of my script. My question is, How do I return the exit code from apt-get in this case? #!/bin/bash install_auto() { apt-get -h > /dev/null 2>&1 if [ $? -eq 0 ] ; ...
4
votes
2answers
264 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 ...
3
votes
1answer
195 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 ...
2
votes
2answers
214 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 ...
1
vote
1answer
276 views

how to locally redefine 'command_not_found_handle'?

I'd like to make a particular bash script failfast when it cannot find a command, while retaining globally the usual friendly command_not_found behavior. E.g., if I save the following to /tmp/foo.sh, ...
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 ...
3
votes
1answer
421 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 ...
5
votes
2answers
184 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 ...
8
votes
3answers
404 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 ...
6
votes
3answers
205 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 ...
3
votes
2answers
356 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 ...
5
votes
3answers
927 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 ...
4
votes
2answers
263 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 ...
2
votes
2answers
122 views

Is there any way I can fit this into my ~/.bashrc as a function?

I just discovered this useful bit of code on this useful-looking website. #!/bin/sh exec tclsh "$0" ${1+"$@"} proc main {} { set lines [lrange [split [read stdin] \n] 0 end-1] set count ...
1
vote
1answer
238 views

Switching source and destination (or undoing the mv, cp operation)

mv or cp commands both expect source and destination as arguments. In case you want to undo the change you made, or just change the source and destination you supplied before, what is the quickest ...
1
vote
2answers
1k views

Script to create files in a template

I just wrote a function in my ~/.bashrc that will let me create a folder for a new website with one command. The function looks like this: function newsite() { mkcd "$*" # mkdir and cd into it ...
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 ...
7
votes
2answers
725 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 (), ...
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: ...
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 ...
4
votes
2answers
270 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 ...
2
votes
3answers
356 views

Prefix every argument with -o in BASH

How do I prefix -p to every argument passed to my function? Modifying the arguments themselves and creating a new array are both fine.
4
votes
4answers
940 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 ...
1
vote
3answers
2k views

alias or bash function does not work

When I create alias wd='ps -ef | grep java | awk {'print $2 " " $9'} | egrep "(A|B|C|D)"' or function wd () { ps -ef | grep java | awk '{print $2}' ... } in my .bashrc file, I get errors. ...
1
vote
4answers
2k 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 ...
4
votes
4answers
659 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 ...