Stack Exchange Network

Stack Exchange network consists of 175 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
25 views

Why shell script function can't return negative number? [duplicate]

When I use shell script , function can't return negative number. Why is that please?
1
vote
1answer
17 views

MacOS: Changing screen capture location

I make a lot of presentations that involve many screenshots, and I want an easier way to organize them by project. I'm trying to write a simple function that changes the location where screenshots are ...
0
votes
1answer
34 views

Accessing function documentation

Is there a way to access docstrings in Bash? How does one include a docstring in a function's definition in Bash? More specifically how do I add and access docstrings to the following function? ...
-1
votes
2answers
72 views

sudo without sudo, implying sudo in script

I made some scripts containing some functions which by design needs sudo permission. I have added those path in .bashrc for Linux and .bash_profile for MacOS so that it can be called from anywhere. ...
3
votes
3answers
252 views

Understanding declare in Bash

After reading ilkkachu's answer to this question I learned on declare (with argument -n) shell built in, help declare brings: Set variable values and attributes. Declare variables and give ...
2
votes
1answer
35 views

How to look up zsh completion definitions

Is there a way to lookup (just print the completion definition to stdout) currently loaded zsh completion functions. I understand that they are stored somewhere on my fpath and I could do something ...
0
votes
1answer
26 views

How to pass list of arguments into a function

I want to pass arguments from a loop into a function func. For simplicity let's say we are working with the loop for x in {1..5} do for y in {a..c} do echo $x$y done done I'm just ...
0
votes
1answer
33 views

sudo redirection doesn't work when in a function

I have a script which writes some content into a file with cat and EOF. While this thing works within a bash script, it doesn't work if I put it inside a function. Working code: cat << EOF | ...
5
votes
1answer
429 views

Unexpected behavior of Bash script: First executes function, afterwards executes alias

When I execute the following script #!/usr/bin/env bash main() { shopt -s expand_aliases alias Hi='echo "Hi from alias"' Hi # Should Execute the alias \Hi # Should Execute the ...
3
votes
2answers
618 views

Bash Script Function Return True-False

I want to query the function by returning the value. My codes are as follows; check(){ file=/root/Turkiye.txt local funkx=$1 while read line; do if [ "$line" == "$funkx" ] ...
1
vote
2answers
99 views

How can I change user inside function

I have a bash script on centos7, and I need to execute some commands as different user. But it seems sudo works as expected outside function and didn't work inside bash function. I run script as ssh ...
0
votes
0answers
22 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
67 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
2answers
60 views

set -f inside function

I have a directory with files file1.c, file2.c and file3.c. The command find outputs: $find -name "*.c" ./file1.c ./file2.c ./file3.c Then I would like to use find without the quotes around .*c. ...
2
votes
2answers
87 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
votes
1answer
42 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
78 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
334 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
49 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
736 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
61 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
179 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
34 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
59 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
165 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
67 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
164 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 ...
1
vote
3answers
112 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
73 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 ...
3
votes
1answer
62 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?

I have a function in my .bashrc file. 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 <= limit ; i++)) do ...
-1
votes
1answer
137 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 ...
1
vote
4answers
201 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
412 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
71 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 ...
1
vote
4answers
68 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
6k 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
106 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
648 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
131 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
32 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
96 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
37 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
1k 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 ...
8
votes
2answers
448 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
111 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
191 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
130 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 ...