The tag has no usage guidance.

learn more… | top users | synonyms

2
votes
4answers
27 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 ...
5
votes
1answer
46 views

Cannot grep jobs list when jobs called in a function

I can grep the output of jobs, and I can grep the output of a function. But why can't I grep the output of jobs when it's in a function? $ # yes, i can grep jobs $ jobs [1]+ Running vim ...
9
votes
4answers
906 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 ...
0
votes
1answer
21 views

netcat daemon for calling functions in sh script

I'm new to shell programming and I have created a script that opens a connection to a server of mine. I want to have this script listen for an input from a client node and use that to run a function. ...
2
votes
2answers
31 views

Get specific result from function

Is there a way to return a specific value in an echoing function? return allows me to return an exit status for the function. I need to return a more sophisticated data structure such as an array, or ...
241
votes
14answers
31k 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 ...
13
votes
4answers
10k views

Executing user defined function in a find -exec call

I'm on Solaris 10 and I have tested the following with ksh (88), bash (3.00) and zsh (4.2.1). The following code doesn't yield any result: function foo { echo "Hello World" } find somedir -exec ...
0
votes
0answers
14 views

How do I compute the angle of the 2D 3 points ? [[ In kernel space]

I 'm looking at the touch driver 's Kennel. Sometimes a touch is bouncing County singah appear. Collected by recruiting three points 'm trying to calculate the angle between . But there is no such ...
0
votes
0answers
23 views

How do i run multiple scripts within another script in the same directory?

Last part of a project and I've created 3 scripts already that do the following, but I now need to run them within a 4th script as if it was one program. Below are the instructions as well as the code ...
-1
votes
0answers
24 views

.bashrc - Create an alias for SSH declare Function [duplicate]

So, I have a function: function () { command1; command2; } When I do ssh root@server "$(declare -f function) function parameter" It works fine. So, I want to make it simplier - through an ...
1
vote
2answers
30 views

Optional parameters in bash function

I have a function for quickly making a new SVN branch which looks like so function svcp() { svn copy "repoaddress/branch/$1.0.x" "repoaddress/branch/dev/$2" -m "dev branch for $2"; } Which I use to ...
1
vote
1answer
34 views

how to get or reflect the name of the bash function which is called? [duplicate]

i did not yet found a solution to this. Anyone a hint? i sometimes write bash functions in my shell scripts and i love to have my scripts being verbose, not just for debugging. so sometimes i would ...
5
votes
3answers
2k 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
0
votes
0answers
6 views

Removing items in one array based on whats in another array - python [migrated]

I'm trying to make a function that removes items from all_cards based on whats in the iss_answer array. My iss_answer array will change often in my program so I can't just type ...
1
vote
1answer
24 views

Which shells have functions where “local” does not alter exported variables for child processes?

In the example below, an exported variable is re-set as local in a function. Bash, Zsh, Fish don't pass on the original value to the child process. Are there any shells that make local affect the ...
3
votes
2answers
49 views

How to make a function available to the command `parallel` (GNU)?

In Bash, let's consider a function that does nothing but echo the argument followed by "is an integer". f () { num="${!1}"; echo $num is an integer; } number=12 f number # 12 is an integer I would ...
1
vote
1answer
36 views

How can I source several files into my .bashrc?

I want to write my functions each in separate files, for easier version control, and source the whole lot of them in my .bashrc. Is there a more robust way than e.g. . ~/.bash_functions/*.sh ?
0
votes
0answers
21 views

Listing available functions [duplicate]

I have a function defined on my .bashrc that allows me to mkdir and cd into a folder, which I call mkcd. I could write the same thing using an alias, or as a script. For aliases and scripts, I can ...
0
votes
2answers
34 views

bash script function argument problem [duplicate]

Not sure why this is producing error. This is a test code emulating my real code. I want to write a wrapper for find and want to allow for any argument, so I'm wrapping each arg in single quotes. ...
3
votes
1answer
33 views

zsh: Tab completion for function with Git commands

In zsh I am using the following function to delete a local and a remote branch with one command: gpDo () { git branch -d "$1" && git push --delete origin "$1" } Currently, ...
3
votes
3answers
75 views

Executing a Bash Script Function with Sudo

I have a script that does a number of different things, most of which do not require any special privileges. However, one specific section, which I have contained within a function, needs root ...
1
vote
2answers
114 views

POSIX print function definition

Bash can print a function defintion: $ bash -c 'y(){ echo z; }; export -f y; export -f' y () { echo z } declare -fx y However this fails under POSIX Bash, /bin/sh and /bin/dash: $ bash --posix ...
4
votes
2answers
122 views

show only physical disks when using df and mount

When I use df or mount, I'm most of all interested in physical disk partitions. Nowadays the output of those commands is overwhelmed by temporary and virtual filesystems, cgroups and other things I am ...
14
votes
8answers
26k views

Doing simple math on the command line using bash functions: $1 divided by $2 (using bc perhaps)

Sometimes I need to divide one number by another. It would be great if I could just define a bash function for this. So far, I am forced to use expressions like echo 'scale=25;65320/670' | bc but ...
1
vote
1answer
90 views

Bash FUNCNAME equivalent in Dash

Bash can print the current function name: $ bash -c 'g(){ echo $FUNCNAME; }; g' g However Dash cannot use FUNCNAME: $ dash -c 'g(){ echo $FUNCNAME; }; g' It is possible to access the current ...
4
votes
3answers
106 views

Using “@” as a bash function name

Is it possible to use the "@" symbol as a function name in a bash script? The following does not work: function @() { echo hello }
0
votes
1answer
30 views

Pipe encrypted archive to uploader

Assume, I do archive several files with this functions: gen_password () { gpg --gen-random 1 "$1" | perl -ne' s/[\x00-\x20]/chr(ord($^N)+50)/ge; ...
3
votes
3answers
101 views

How to determine callee function name in a script

To make it short, doing something like: -bash$ function tt { echo $0; } -bash$ tt $0 will return -bash, but how to get the function name called, i.e. tt in this example instead?
1
vote
2answers
55 views

How do you list all functions and aliases in a specific script?

I have a bash script that's getting quite long. It would be nice if I could list all the functions in it. Even better would be listing the name of the function and any documentation about it's ...
3
votes
3answers
165 views

Recursive Function not Working

I know that this is a ridiculous idea, but I'm trying to get a script to work that branches through every directory on the file system. The file's name is "Everywhere.sh". Here's the code: ...
37
votes
5answers
6k 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 ...
0
votes
1answer
24 views

display array in a function - not working

What am I missing here? I have created a simple array: declare -a appArray=( "item1 -a -b" "item2 -c -d" ) If I echo this I can see it all echo ${appArray[@]} > item1 -a -b item2 ...
5
votes
4answers
430 views

How to use call-by-reference on an argument in a bash function

I am trying to pass a "var name" to a function, have the function transform the value the variable with such "var name" contains and then be able to reference the transformed object by its original ...
4
votes
3answers
3k views

forward function and variables into sudo su - <user> <<EOF

I have declared functions and variables in bash/ksh and I need to forward them into sudo su - {user} << EOF: #!/bin/bash log_f() { echo "LOG line: $@" } extVAR="yourName" sudo su - ...
3
votes
4answers
261 views

How to create a function that can sort an array in bash?

I'm trying to figure out how to make a function that can take an array as a parameter and sort it. I think it is done with positional variables, but I'm not sure.
3
votes
2answers
166 views

How do I remove all but the file name (with no extension) from a full file path? [duplicate]

I am presently writing a Bash function to convert all the man pages listed by equery files <PACKAGE> | grep /usr/share/man/man (if you are unfamiliar equery is a tool used on Gentoo-based ...
3
votes
2answers
468 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 ...
7
votes
1answer
439 views

Bash alias with a space as a part of the command

I'm trying to create a bash alias, where the alias itself has a space in it. The idea is that the alias (i.e. con) stands for sudo openvpn --config /path/to/my/openvpn/configs/. Which results in a ...
1
vote
3answers
91 views

Bash (Git) - Functions, Alias, and SH script files

I have gotten quite prolific with the use of the aliases, especially with all the different git commands and their order and interdependencies etc. So, I've created a few alias that run more complex ...
1
vote
1answer
29 views

Difference between writing `function Name { …; }`, `Name () { …; }` and `function Name () { …; }` in bash [duplicate]

You can write a bash functions several ways: function JoinStrings { ...; } Or function JoinStrings () { ...; } Or JoinStrings () { ...; } Is there any difference between these ...
1
vote
1answer
37 views

How to emulate returning arbitrary values from shell functions?

Shell functions are just statements and they don't return values. Can anyone share best practice on writing functions that return values in bash? Let's say I've a function that joins two strings: ...
1
vote
1answer
25 views

Function for archiving arbitrary files with encryption

I'm no so advanced in bash so can not make my function work properly. Here is the code: archive() { for f in $PWD do for ((i=1; i++;)) do 7za a "$1".7z $f -pSECRET -mhe done ...
0
votes
2answers
41 views

Script not able to generate files for backup file

I'm new to shell scripting and I trying to write a script that: Prompts for a directory Loops through the directory If it find files it then backs them up using a function in the script Creates a ...
2
votes
2answers
209 views

Shell valid function name characters

Using extended Unicode characters is (no-doubt) useful for many users. Simpler shells (ash (busybox), dash) and ksh do fail with: tést(){ echo 34; } tést But bash, mksh, lksh, and zsh seem to ...
3
votes
2answers
2k 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 ...
0
votes
1answer
54 views

Shell script using function to display system information sending errors

I'm trying to write a script to keep track of my system information. I want to use "function" in the script and just call the functions out. I'm having trouble with the commands working in the ...
0
votes
2answers
80 views

Shell script using function () not finding command

I'm learning shell scripting and I'm studying how to use function in the shell script. The script is suppose to run as a basic math calculator with two defined numbers by the user. It's suppose to ...
11
votes
2answers
3k views

How to get current buffer's filename in emacs?

One of the main features I miss about Vim, is that it always saves the filename of the current file in the % buffer (more info). That allows launching commands easily like: ;; compile current file :! ...
2
votes
1answer
38 views

Is there any danger to using an unset variable in a bash function definition?

Doing some code refactoring, and I realized I don't know if this matters at all: The function definition is going to be sourced from another file (a sort of library). The function uses certain ...
2
votes
3answers
136 views

Source only part of a script from another script?

This would probably never be the BEST approach to something, but I'm wondering if it's even possible. Something like: awk '/function_i_want_to_call/,/^$/{print}' script_containing_function | xargs ...