The tag has no usage guidance.

learn more… | top users | synonyms

1
vote
3answers
37 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?
9
votes
4answers
793 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 ...
1
vote
2answers
24 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
155 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: ...
35
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
19 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 ...
231
votes
13answers
29k 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 ...
5
votes
4answers
277 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 ...
3
votes
3answers
2k 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 - ...
4
votes
4answers
201 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
131 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
451 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 ...
8
votes
1answer
238 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
63 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
27 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 ...
2
votes
1answer
33 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: ...
2
votes
1answer
22 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
25 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
124 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
1k 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
39 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
49 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 ...
10
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
33 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
123 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 ...
0
votes
1answer
14 views

Creating a function that reads off an input list of files

I am new to UNIX/Linux and am in the process of writing bioinformatics pipelines. These pipelines take in input files and pass them through multiple packages. Say there is a list of files that goes ...
2
votes
1answer
823 views

Value assigned inside a function variable is always empty

I have the following simple script. In this script, I am assigning a value to a global variable inside a function. I can clearly see that the value is being assigned to the variable via a debug ...
3
votes
1answer
223 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 ...
6
votes
1answer
123 views

Function to simplify grep with an often used log

First off, sorry if this is a painfully newbish question! I'm trying make a function that simplifies grepping a log I have to work with on a regular basis. I'd like to use extended regexp with it ...
1
vote
2answers
87 views

Returning a variable from a function [closed]

I have the Linux script shown below. I can get it to return from the method decrypt nothing which I need to unzip a file. The method decrypt sends a string with the name of a zip file. Please give ...
0
votes
2answers
269 views

Bash Scripting echo locally in a function

In bash scripts I try to keep my variables local to functions wherever I can and then pass what I need out of functions like bellow #!/bin/bash function FUNCTION() { local LOCAL="value" echo ...
1
vote
2answers
51 views

Cursor position in vi at opening of the file

vishex () { echo '#!/bin/bash' > $1; chmod +x $1; vi $1 } The goal of the above function is to have an alias for fast and comfortable creation of bash scripts. I would like that at ...
2
votes
3answers
188 views

Bash function with `getopts` only works the first time it's run

I defined the function f in Bash based on the example here (under "An option with an argument"): f () { while getopts ":a:" opt; do case $opt in a) echo "-a was triggered, ...
1
vote
1answer
149 views

bash: syntax error near unexpected token

I'm trying to create a bash alias alias backlight='__backlight () { echo "$@"; cd ~/Code/MSI-Backlight; sudo nodejs ~/Code/MSI-Backlight/msi-backlight.js "$@"; }', it works fine with no parameters but ...
0
votes
2answers
37 views

change permissions to a file and everything inside , recursively

So I want to create an alias called changeAllPermisions that accepts one parameter argument in such a way that when changeAllPermissions argument is called , both Group and Other do not have access to ...
0
votes
1answer
72 views

Calling php function from /var/www/folder (as www-user), which is located in a file in /var (or any folder) (with root permissions?)

I wrote a script which "walks" through /var/www/xyz and scans folders and more. The folders are all 'user-bound', so the permissions for a single folder there are set to the specific user. Running ...
0
votes
0answers
21 views

Lttng tracepoint(provider,tpname, arg)

I want to insert lttng - tracepoint("traceprovider_name"tracepoint_name" "$1"$2") statements with same tracepoint_name and traceprovider_name but with different arguments: for example: 1)How to ...
0
votes
2answers
58 views

Returning local values from Bash variables?

I studied this article called Returning Values from Bash Functions. Data Lorem. \begin{document} hello \end{document} Case #1 which does not work Code #!/bin/bash function getStart { ...
0
votes
2answers
77 views

How to increment local variable in Bash?

Data 1 \begin{document} 3 Code #!/bin/bash function getStart { local START="$(awk '/begin\{document\}/{ print NR; exit }' data.tex)" echo $START } START2=$(getStart) echo ...
3
votes
1answer
40 views

functions arguments

I am having trouble with what should be a simple bash script. I have a bash script that works perfectly: function convert_to () x_max=2038 y_max=1146 x_marg=100 y_marg=30 x_grid=150 y_grid=150 if ...
13
votes
7answers
23k 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 ...
27
votes
7answers
16k 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: ...
7
votes
1answer
897 views

How does VARIABLE=() { function definition } work in bash

I have seen something like this in my bash ENV: module=() { eval `/usr/bin/modulecmd bash $*` } How does this construct work? What is it called? I'm not asking about modulecmd, I am asking about ...
2
votes
2answers
167 views

Execute only if it is a bash function

I'm looking for something similar to Bash's built-in command that will only run something if it is a function. So currently I have an insecure way of doing: # Go through arguments in order for i in ...
1
vote
2answers
395 views

How to call in a kernel level function in user space [closed]

I've developed some helper functions in the kernel. They're called by other functions in the kernel. Currently, they make my custom kernel panic :( For now (i.e. debugging), I made them as a passive ...
1
vote
0answers
45 views

Can't get bare-bone simple bash function to work [closed]

I have some tmux scripts in a directory, and I'm trying to make a script to make it easier to call them. Function is below tms() { ~/linux/scripts/tmux-scripts/$1 } This is in my bashrc, I then ...
1
vote
4answers
84 views

Problem with entire function in a script

I want to detect online network/shell services in my Solaris. I write following script for this purpose: compare_ser() { if [ "$1" != "" ]; then echo "True" >> Solaris.txt fi } export -f ...
1
vote
1answer
87 views

sh - Using Arguments in .profile functions

I want to use an argument in the function I created in my .profile file. I want to ask for input if no argument is given, otherwise set a variable to $1. When I check $1 to see if it is empty, I ...
2
votes
4answers
270 views

locate alias with find

On small systems where there is no locate installed, How would an alias look like that gets the same result as locate? I can imagine find can produce the same output so an alias could look like ...
1
vote
3answers
126 views

Using a function defined in a parent script

I wrote the following script in the test.sh: #!/bin/sh compare() { if [ $1 != root ]; then echo "Fail" >> CAT1.txt fi } awk -F: '$4 == 0' /etc/passwd | cut -d: f1 | xargs -n1 -i bash -c ...