Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [function]

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.

0
votes
0answers
10 views

Run a function in a Shell Script as a different user [duplicate]

I have a .sh script which contains a set of functions. I have made the script executable (chmod 777). If I want to run a function as my current user I simply do: . /path/to/script/functions.sh &&...
0
votes
0answers
18 views

pass function to nohup to keep command running

I have the following function set up which looks at a given directory and decompresses any files using qpress. It also records how long the process runs for. fc_decompress () { startTime="$(date +...
0
votes
1answer
25 views

set -f inside function

I have a directory with files file1.c, file2.c and file3.c. The output of find is: $ find -name "*.c" ./file1.c ./file2.c ./file3.c I want to remove the quotes from the argument of -name. So I use ...
2
votes
2answers
80 views

Add to array only within scope of function

I'm writing a function that will make a REST API calls which could be either GET, PUT, DELETE, POST, etc. I would like to feed this method to the function as a parameter and add it to the options ...
1
vote
0answers
14 views

Calling a c library function from within ctypes python [migrated]

Using nm, I get these functions back from my library: 0000000000004bc0 T __Z13generator_sind 0000000000004120 T __Z13librador_exitv 0000000000004080 T __Z13librador_initv 0000000000004b70 T ...
-1
votes
1answer
34 views

Shell Function: Sequence of Pipelines as Argument

I have a shell function (in .bashrc) which creates a temp file, executes the arguments (including all sequence of pipelines), redirects it to a temp file and then open it in VS Code. I invoke the ...
4
votes
3answers
70 views

Define bash function which does not show up in xtrace (set -x)

In a bash script I have a log() function that is used in several places, as well as a logs() function that diverts lots of line to log(). When I run parts of the script with set -x, obviously all ...
1
vote
0answers
42 views

Define function without name and immediately call it [duplicate]

I have a script file (it's .bashrc actually), in this file I have a lot of variables and functions which are used only inside this script, but these variables and functions are publicly visible for ...
0
votes
1answer
49 views

How to count the number of arguments passed under condition?

I'm trying to create a shell script taking as argument a series of averages and assign for each note an observation, example : There are 5 averages that are introduced. There are 2 notes that are: ...
1
vote
2answers
47 views

Do we need to define the shell on file that include only functions?

I am wondering if we need to add shell title: #!/bin/bash on a script, second.sh, which only defines a function, and is called from another script, script.sh. For example, with script.sh containing ...
7
votes
4answers
655 views

Viewing man pages in vim

I wrote a function in bash to see manpages in vim viman () { man "$@" | vim -R +":set ft=man" - ; } This works fine, the only problem occurs if I pass a manpage to it which doesn't exist. It prints ...
1
vote
0answers
57 views

Why do I have so many (~500) shell functions defined? [closed]

By running: $ declare -F | wc -l 488 I found a lot more functions than I was expecting. Examples: declare -f __docker_append_to_completions declare -f __docker_complete_capabilities_addable ...
1
vote
3answers
83 views

bash array with space in element [closed]

I have a text log file $ cat aaa 673 20160405 root "/path_to/gis/20160401/20160301_placement_map_org.dbf" "" 673 20160405 root "/path_to/gis/20160401/...
0
votes
1answer
29 views

In Linux, how can I set a Function globally

In AIX, I have a, FPATH set function, which is globally accessible by just calling the function name. I am trying to replicate this functionality in Linux. I set the function in $PATH variable, I ...
1
vote
1answer
55 views

How to make my bash function known to external program

I have bash functions foo and bar in my ~/.bashrc. Function foo calls an external command ext_command that itself takes as one of its arguments another command. I want to pass bar as that command, i....
2
votes
2answers
102 views

Writing a script containing just one function definition v.s. moving the code in function body to the script?

Some background: When writing reusable code in bash, I saw some shell scripts containing several definitions of functions. Sometimes I can't figure out if the functions defined in a script are ...
0
votes
2answers
63 views

How can I wrap this checking of variable set/unset into a function?

As https://stackoverflow.com/a/13864829/ said, $ if [ -z ${aaa+x} ]; then echo "aaa is unset"; else echo "aaa is set"; fi aaa is unset can test if a variable aaa is set or unset. How can I wrap ...
2
votes
1answer
162 views

Does the system-wide limit on argument count apply in shell functions?

The other question asks about the limit on building up commands by find's -exec ... {} +. Here I'd like to know how those limits compare to shells' inner limits. Do they mimic system limits or are ...
0
votes
3answers
73 views

SSH with Command Doesn't Run as an Alias

I have the following command to remote into a local server and tail -f the latest log file for an application that I have. The command works perfectly fine from the command line - ssh user@hostname ...
0
votes
1answer
56 views

Why does zsh replace hyphens with underscores in these functions?

I've got the following function aliases sourced in zsh and bash consoles: compose() { docker-compose $* } run() { compose "run --rm app $*" } rails() { run "rails $*" } In bash, running ...
2
votes
1answer
58 views

How do you reuse a script in another?

Is it correct that there are two ways to reuse a script A in another script B: script B invokes script A as a command (B creates a child process to run A) organize the code in script A is organized ...
10
votes
2answers
2k views

Can you explain these three things in this bash code for me please?

I have a function in my .bashrc file and I can't wrap my head around it. I know what it does, it steps up X many directories with cd Here it is: up() { local d="" limit=$1 for ((i=1 ; i &...
-1
votes
1answer
124 views

How can I add find exec command into bashrc?

find . -name "*.mov" -exec bash -c 'ffmpeg -i "{}" -c:v libx264 -crf 20 -pix_fmt yuv420p "${0/.mov}.mp4"' {} \; Above command works perfectly when I run it manually but I could not put it within a ...
0
votes
3answers
121 views

Define function in fish, use it with watch

I want to define a function, and call that function every n seconds. As an example: function h echo hello end Calling h works: david@f5 ~> h hello But when using watch, it doesn't... watch ...
2
votes
4answers
365 views

What exit modes exist in shell-scripting in general and in Bash in particular?

I know that in shell-scripting an "exit" usually means voluntarily or at least successfully terminating a session (or a process within a session) and that there are several different exit modes; the ...
0
votes
1answer
65 views

Can an array be the first argument to a function? [duplicate]

I have a function that one of the arguments is an string that has space separated words. E.g. "foo bar ccc" I think this kind of string can be treated as an "array" and loop over each word. My ...
20
votes
7answers
4k views

Is the “callback” concept of programming existent in Bash?

A few times when I read about programming I came across the "callback" concept. Funnily, I never found an explanation I can call "didactic" or "clear" for this term "callback function" (almost any ...
8
votes
3answers
2k views

What is the difference between the terms “program”, “command” and “function” in Linux & Unix?

I would like to know whether the commands that we call in the shell are functions or programs?
1
vote
4answers
66 views

Pass function argument to defined variable

How can I "inject" a function argument to a defined variable like in this example? mood="i am $1 happy" happy () { echo "$mood" } happy "very" Current output: i am happy Desired output: i ...
13
votes
2answers
5k views

Why is $1 in a function not printing the script's first argument?

Why doesn't echo $1 print $1 in this simple bash script? #!/bin/bash # function.sh print_something () { echo $1 } print_something $ ./function.sh 123 -> why doesn't it print '123' as a result?
1
vote
3answers
71 views

Possible to declare function locally and use it at the remote end of an SSH connection

I am looking to create a function in a script locally, and call it on a remote host, over an SSH connection, is this possible? set -e gitpull () { cd /var/www/html STATUS=$((git status) 2>...
2
votes
2answers
488 views

Syntax error while calling a function

So I tried making a function in a script that creates a new variable for each argument when running the script. This is my code: #!/bin/bash # Creating function log #ARG1=${1} log() { echo "You ...
0
votes
1answer
99 views

Role of export -f statement when creating functions in bash_profile

I came across a bash_profile file that makes use of the export -f statement in the following manner: # Run xelatex and BibTeX function xelatex_bibtex { xelatex -shell-escape "${1}.tex" && ...
0
votes
1answer
30 views

Using Multiple Function to get an output in a single Line

I am trying to create output which keeps hostname,ssh protocol and root login information with bash script. And I would like to do it with developing functions. I developed .sh but it does not work. ...
0
votes
2answers
71 views

Using shell 'printf' where the format string is in a variable and doesn't have a fixed number of field placeholders?

I am writing a shell function which makes an external API call via cURL (the external API syntax isn't under my control). I've approached it like this (simplified): #!/bin/sh template_get_entry='...
0
votes
0answers
34 views

What is the correct way to assign a function containing chained commands to an alias in .bashrc?

I wish to invoke an alias as follows: alias_name arg0 arg1 arg2 arg0 = filename arg1 = General string arg2 = String representation of last command The aim is that by calling the alias and passing ...
8
votes
3answers
887 views

Shell: Using function with parameters in if

I'm trying to execute the code below but when I try to use my function in the if statement I get the -bash: [: too many arguments error. Why is it happening? Thank you in advance! notContainsElement ...
7
votes
2answers
387 views

function's calling context in zsh: equivalent of bash `caller`

In bash, I can write: caller 0 and receive the caller context's: Line number Function Script name This is extremely useful for debugging. Given: yelp () { caller 0; } I can then write yelp to ...
2
votes
1answer
83 views

Detect optional function argument (array)

Consider this function: function add_one(in_ar, each) { for (each in in_ar) { in_ar[each]++ } } I would like to modify it such that if a second array is provided, it would be used instead ...
1
vote
2answers
143 views

find inside shell function

I try to put find inside function and catch an argument passed to this function with the following minimal work example: function DO { ls $(find . -type f -name "$@" -exec grep -IHl "TODO" {} \;) ...
2
votes
1answer
108 views

Command don't return expected value inside .zshrc

Trying to run: function which_terminal { return (ps -p$PPID | awk "'NR==2'" | cut -d "' '" -f 11) } inside .zshrc to get a varible with which terminal emulator is running so I can configure ...
3
votes
1answer
116 views

overwrite and reuse existing function in zsh

I would like to slightly extend a zsh completion function. I would like to avoid putting the complete function body into my homedir with only one line changed. Instead I would like to intercept it's ...
10
votes
4answers
2k views

call function declared below

Is it possible to call a function which is declared below in bash? Example if [ "$input" = "yes" ]; then YES_FUNCTION elif [ "$input" = "no" ]; then NO_FUNCTION else exit 0; fi ...
0
votes
2answers
62 views

how can I can provide an array as a positional parameter to a function or script?

In my script, I found some operations on an array are reusable. So I am considering to refactor the reusable code into a function or a script. However how can I write a function or script so that I ...
3
votes
2answers
514 views

How shall I reuse a function in multiple scripts?

In bash, sometimes I would like to reuse a function in several scripts. Is it bad to repeat the definition of the function in all the scripts? If so, what is some good practice? Is the following way ...
2
votes
1answer
43 views

For loop not working in a function with arguments

So I'm creating a function that does a for loop in all the files in a directory as a given argument and prints out all the files and directories: #!/bin/bash List () { for item in $1 ...
1
vote
2answers
52 views

Pump command output as function argument

I have this extremely simple function in my script: # Used for debug tracing. log() { : echo "log: $1" } The idea is to be able to customize/turn off logging at a single place. Very crude. ...
1
vote
1answer
42 views

Problem with shellscript crashing after “exec kill -SIGINT”

I have modified a shell script i found here: https://github.com/Slympp/ConanLinuxScript But im having troubles with the function "conan_stop" The script just terminates after exec kill -SIGINT $pid ...
0
votes
1answer
72 views

shell script: Call variable with parameters/arg

I have the following function: testcur.sh : #!/bin/bash function valcurl { if [[ $1 != "" ]] then tbl=$2 # can be multiple values data=/home/data btic=$data/$tbl"_btic" ...
2
votes
1answer
100 views

How to start Vim from a trap and still be able to resume it after suspending it?

I have the following code in my ~/.zshrc: nv() ( if vim --serverlist | grep -q VIM; then if [[ $# -eq 0 ]]; then vim elif [[ $1 == -b ]]; then shift 1 IFS=' ' vim --...