Questions on function usage in the context of Unix & Linux (mostly but not exclusively shell scripts). Questions on programming in Python, Perl, Ruby, etc. should be asked on Stack Overflow.

learn more… | top users | synonyms

1
vote
1answer
37 views

Declare as local var will break a function and log out “1: number expected”

function projectopen { local di_files=(*.xcworkspace */*.xcworkspace *.xcodeproj */*.xcodeproj) # open first exsit file ls -d -f -1 $di_files 2>/dev/null \ | head -1 \ | xargs ...
2
votes
2answers
34 views

Applying bash function to each file in subfolder recursively

I am trying to write a script that will apply a bash function for each file in a directory recursively. For example if the directory tests had all my files and sub-directories in it, the script find ...
0
votes
1answer
25 views

why doesn't eval declare in a function work in bash?

Tracking down strange behavior a bash script resulted in the following MWE: set -o errexit set -o nounset set -x my_eval() { eval "$1" } my_eval "declare -A ASSOC" ASSOC[foo]=bar echo success ...
3
votes
1answer
44 views

How can I print out the complete function declaration of any function which has a specific string inside it?

I have lots of functions in my bashrc, but for newly created ones I often forget the name of the function. So for example, when I have defined this function in my .bashrc: function gitignore-...
3
votes
2answers
81 views

Call a .bashrc function from a bash shell script

I want to be able to name a terminal tab so I can keep track of which one is which. I found this function (here) and put it in my .bashrc: function set-title() { if [[ -z "$ORIG" ]]; then ORIG=$...
1
vote
2answers
47 views

Bash function assign value to passed parameter

I've got the following situation: I'm writing a script that will read its parameters either from a config file (if exists and parameter present) or asks the user to input said parameter if it's not ...
0
votes
1answer
40 views

resume running a script after function call

I am writing a script which displays input options in a while loop provided by a function user_input() and set values depending on user input, then I call another function user_info(). If a user made ...
0
votes
2answers
48 views

how to pass echo statement output as argument to function?

logfile=./testlog `touch ${logfile}` function logger() { echo "[${USER}] [$nowTimestamp] [INFO] ${1}" >> ${logfile} } echo "started executing script" | logger I just want to pass above ...
0
votes
0answers
31 views

Using spd-say in a bash script function

I'm sure this is fairly elementary, but I can't figure it out being as I'm mostly a complete beginner to the nuances of linux shell scripting. Perhaps someone would like an easy one? My script: #!/...
1
vote
1answer
45 views

How to split Bash array into arguments

I wrote a bash script for listing python processes, ram usage and PID and status in human readable form with colored lines. But I have some trouble with working time of the script. Because of the ...
40
votes
11answers
4k views

Why write an entire bash script in functions?

At work, I write bash scripts frequently. My supervisor has suggested that the entire script be broken into functions, similar to the following example: #!/bin/bash # Configure variables ...
1
vote
2answers
45 views

bash function : splitting name and extension of a file

I have the following function split in my .bash_profile file. function split { name="${$1%.*}" ext="${$1##*.}" echo filename=$name extension=$ext } Now I should expect that the command ...
1
vote
1answer
24 views

Bash function with arguments

I know I can write bash scripts like: foo() { echo $1; } but can I define a function that writes: foo(string) { echo $string; } I just can't find my way out of this.
0
votes
1answer
36 views

Function added to .bashrc doesn't work [duplicate]

I've added this function to my .bashrc file: SSH(){ ssh -X -o PasswordAuthentication=yes [email protected].$* } and while trying to perform for ex. SSH 101 I get message: SSH: command not found ...
1
vote
2answers
44 views

How to improve function for simplifying history command

I came up with this snippet to simplify use of history and prevent flooding of the scroll buffer: h() { if [ $# -eq 1 ]; then history | grep $1 | tail -n $(expr $(tput lines) - 1) ...
1
vote
1answer
67 views

bash function to execute a command as argument

I'm writing a bash script and I have a function that gets 3 arguments, a hostname, a command, and a file, it should excute the command on that hostname and redirect the output to the filename. This is ...
1
vote
2answers
38 views

Forbid use of specific commands

I develop tools on a cluster. Each user loads common environment files in their .bash_profile. I have a login node on which certain tools should not run. How can I prevent users from using those tools ...
1
vote
1answer
26 views

How to execute 'find' with 'sed' within a bash function

I'm trying to write a simple bash function to search-and-replace recursively down a directory, changing one string to another. Here's what I've got: function sar () { from="$1" shift to="$...
0
votes
1answer
46 views

System/ X freezes graphically on function keys press

Not entirely sure why, but shortly after running a system upgrade, ie. sudo pacman -Syyu, it appears that my entire graphical environment freezes on the keypress of any of half my function keys, such ...
1
vote
1answer
17 views

Loop to apply command to 300 files in a directory and rename the output including the original input file name plus new text

I have a code that is svr_vars. I have a directory with 300 different files. I want to loop it and to run the code on each file in the directory. The svr_vars code outputs a generic filename of ...
19
votes
3answers
1k views

Do functions run as subprocesses in Bash?

In Advanced Bash-Scripting Guide, in example 27-4, 7-th line from the bottom, I've read this: A function runs as a sub-process. I did a test in Bash, and it seems that the above statement is wrong....
3
votes
1answer
75 views

Circular name references in bash shell function, but not in ksh

I'm writing a set of shell functions that I want to have working in both Bash and KornShell93, but with Bash I'm running into a "circular name reference" warning. This is the essence of the problem: ...
1
vote
2answers
38 views

How do I reference an original command, so I can replace it with a function

So I am trying to create a simple function to replace the standard who command with my own, similar to a function I use to replace the standard cd command. Goal: Replace original who command with who ...
2
votes
1answer
169 views

Bash function that accepts input from parameter or pipe

I want to write the following bash function in a way that it can accept its input from either an argument or a pipe: b64decode() { echo "$1" | base64 --decode; echo } Desired usage: $ b64decode ...
2
votes
0answers
85 views

Exported bash functions sometimes visible from Perl

My Redhat 9, OpenBSD 4.9, FreeBSD 10, Macos X, LinuxMint 17.3, and Ubuntu 14.04.4 all print OK when running this: myfunc() { echo OK; } export -f myfunc perl -e open\(\$fh,\"\|-\",\"@ARGV\"\)\;close\$...
4
votes
2answers
197 views

Return on error in shellscript instead of exit on error

I know that set -e is my friend in order to exit on error. But what to do if the script is sourced, e.g. a function is executed from console? I don't want to get the console closed on error, I just ...
1
vote
2answers
76 views

How can you pass parameters to function in a script?

I'd like to write a function that I can call from a script with many different variables. For some reasons I'm having a lot of trouble doing this. Examples I've read always just use a global variable ...
1
vote
1answer
58 views

Func name as variable in loop

Overview: I save my variable in a config file and call them later. Each entry with the name FailOverVM has a number beside it like FailOverVM1 and I want to check to see if it has data and generate a ...
1
vote
1answer
119 views

Segmentation fault when calling a recursive bash function

I have hundreds of multiple folders which contains thousands of zip files which contain nested within the zip files like show on three below start tree structure 012016/ ├── 2016-01 │   └── 2016-01 │ ...
1
vote
2answers
628 views

Syntax Error near Unexpected Token in a bash function definition [closed]

Forgive me; I'm pretty new to bash files and the like. Here is a copy of my .bashrc: alias k='kate 2>/dev/null 1>&2 & disown' function kk {kate 2>/dev/null 1>&2 & disown}...
1
vote
1answer
62 views

Save return value from a function in one of its own parameters

This is what I want to achieve: Function: Func1() { $1="Hello World" } Call function: local var1 Func1 var1 echo $var1 (should echo Hello World) I found this example which seems to work, but ...
5
votes
1answer
54 views

SIGINT is not cleaned up in “${FUNCNAME[@]}”

Take the following script, interrupted by keyboard input ^C as shown: $ function a() { echo "Performing"; sleep 10; echo "Performed"; } $ a Performing ^C $ echo "${FUNCNAME[@]}" a source If we ...
6
votes
1answer
63 views

Displaying usage comments in functions intended to be used interactively

I have a number of functions defined in my .bashrc, intented to be used interactively in a terminal. I generally preceded them with a comment describing its intended usage: # Usage: foo [bar] # Foo'...
1
vote
1answer
37 views

$* variable of zsh function leads to unexpected results

I have this function (defined inside my ~/.zshrc): function graliases { if [[ "$#*" -lt 1 ]] then echo "Usage: graliases <regex>" else echo "$*" grep -E '*"$*...
1
vote
2answers
36 views

VIM: function that checks if external program is running

Using a vim function, I would like to check if a program is running using pgrep, and if it is not running then do something. In particular, I want to achieve something like this: function! ...
1
vote
0answers
39 views

Returning an array from a ksh93 function

I would like to return an array from a function in ksh93. At this point, using the following method, the contents of the array are passed as a single element even though, in the fonction, the array ...
1
vote
1answer
30 views

Caller-aware function in bash

I have a function (=callee) that should effectively declare and assign a couple of variables in its caller. It should also be able to tell what it's caller's name is. For now I achieve the former by ...
0
votes
1answer
40 views

retrieve numbers with 0 for a single int value

In my script, I need to get a 0 for numbers <=9. When the user types any number, I add 5 to it and if the result is less than 9, I need to print a 0 in order to have a 2-digit number (like 07,08,09 ...
1
vote
1answer
257 views

Script to send mail using function

I am trying to write a bash script with a function which you use to send an email from the command line to an address and include a Cc address, a subject line, and an input file. For example, if the ...
1
vote
0answers
299 views

zsh function: maximum nested function level reached

Consider this simple function I am trying to add in my .aliases (dnf is Fedoras newest replacement of Yum): function abc() { dnf search something } It works well on Bash but I get the following ...
4
votes
4answers
57 views

Does \ work for escaping functions?

I have a function defined in my .bashrc that I would like to bypass: function func() { // func } export -f func When I run env -i func I can access the func command without the function in the ...
0
votes
2answers
290 views

Why should not I update to most recent kernel immediately after release [closed]

Question intended for system administrators. Consider system running a old but working kernel and all the required functionality is available. (Ubuntu 12.04 LTS specifically with kernel 3.2) Then a ...
0
votes
3answers
313 views

Defining bash function dynamically using eval

I'm trying to define a bash function dynamically using following code: delegate_function() { echo "output from delegate"; } eval "parent_function() { echo $(delegate_function); }" The intent is to ...
1
vote
2answers
430 views

Scope of Local Variables in Shell Functions

After reading 24.2. Local Variables, I thought that declaring a variable var with the keyword local meant that var's value was only accessible within the block of code delimited by the curly braces of ...
0
votes
2answers
58 views

Please explain below bash function

I find this function online. It's does creating a directory and changing to directory. But I want to know every part of it. function mkdircd () { mkdir -p "$@" && eval cd "\"\$$#\""; }
1
vote
3answers
321 views

Using time on bash functions (not commands)

How can one measure individual calls to bash functions from inside the bash file. I have a program that I call using the command eclipse -b col_solve.pl -e "myPred" This call outputs some ...
1
vote
3answers
54 views

case statement not behaving as expected (fuzzytime() function)

FuzzyTime() { local tmp=$( date +%H ) case $((10#$tmp)) in [00-05] ) wtstr="why don't you go to bed" ;; [06-09] ) wtstr="I see your very eager to start the day" ...
0
votes
1answer
47 views

Is there a Fish Function I can make to eliminate leading “$”/“#” from commands copied from sites?

Sometimes when I'm copying and pasting a command from a site, I accidently copy the leading "$" or "#" by accident. Is there a Fish Function I could make that would check if one of those is included ...
0
votes
2answers
24 views

ZSH function to edit a file based on an input at the cli

I need to set up a function in zsh that would edit a different file based on some input at the command line. I want to simplify my aliases so I don't have multiple aliases to do the same thing but ...
2
votes
4answers
347 views

How to catch and handle nonzero exit status within a Bash function?

Say I have the following (pointless) Bash function: myfunc() { ls failfailfail uptime } I run it as follows: myfunc || echo "Something is wrong." What I want to happen is ls runs (as ...