The tag has no wiki summary.

learn more… | top users | synonyms

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
2answers
56 views

Call the script after disconnecting from server

I have a script I made for work that will call a function that takes an argument. I use the arguments to ssh into our servers. My question is: Is there a way to call the method so that if/when I get ...
2
votes
1answer
42 views

How can I get this script file's functions to load without having to source it every time? “command not found” (Bash/scripting basics)

How can I get this script file's functions to load without having to source it every time? I created a file foo with script functions I'd like to run. It's in /usr/bin, which is in the PATH. File ...
2
votes
2answers
109 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
56 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
83 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}" ...
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 ...
2
votes
1answer
62 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
23 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
243 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
4answers
133 views

What does “while test $# -gt 0” do?

I'm trying to create a function and believe I found a good working example but I don't understand all the logic behind it. More specifically, on the "while" line, could someone explain what test is ...
3
votes
3answers
131 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 ...
0
votes
1answer
48 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
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 ...
2
votes
3answers
256 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
483 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
21 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
1answer
29 views

Expand variable in function definition in zsh

I want to generate a set of functions in my shell in a for loop, but I can't see how to access a variable inside the function body of the function I'm creating. In essence, I would like the following ...
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
2answers
60 views

Nature of the positional parameters

I am beginner to shell scripting and wrote this script which sort of confused me a bit... function func { the variables received are echo $0: $1 and $2 } echo in the main script func ball boy The ...
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
310 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 ...
0
votes
1answer
31 views

Using variable in KSH function

I tried to have a switch if either an option is set or not while getopts "s:u:d:e:ch" _OPTION; do case $_OPTION in ... c) isCSet="Y" then I'm ...
0
votes
1answer
100 views

Function, return value using pwd in KSH

I tried to write a small ksh script: fDestExists (){ cd /tmp read vANSWER?" >> Do you want to create a repository in pwd ? Type YES or NO" echo " |----> $(fGetDatum) You ...
1
vote
1answer
193 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 ...
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 ...
1
vote
2answers
210 views

How to view source of “string.h” and other library function source code in gedit of Ubuntu?

I remember viewing source code of library function but now I don't know how to view it. Can someone help me viewing source code of library function?
0
votes
1answer
156 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 { ...
0
votes
1answer
110 views

Using break command as argument to function [closed]

What about use so solution? Functions run in loop (cycle?). In that loop - I have another function wuch also uses loop. When second function get NO answer from user - it send break 2 to stop loop and ...
3
votes
3answers
90 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
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 ...
0
votes
2answers
86 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 ...
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?
1
vote
1answer
155 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
542 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 ...
0
votes
1answer
90 views

Converting a loop of code to function

I want to retry a command for 5 times with an interval of 20 seconds. I want this command to be passed as a method parameter. How to do it? And once the function is written how to pass the value to ...
1
vote
2answers
665 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
767 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
274 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
202 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
240 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
284 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 ...
5
votes
2answers
362 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 ...
3
votes
1answer
428 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 ...
1
vote
2answers
168 views

Best way to call command within a shell function having the same name [duplicate]

I like to encapsulate commands within shell-functions using the same name. But to avoid the shell-function calling itself recursively, I specify the complete path of the command as the following ...
5
votes
2answers
466 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 ...