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
9 views

How to edit function from import table in process with gdb

How can I edit function from import table of process to invoke to another address. For example , my process has fopen function in import table , and has read in import table. I want to edit the ...
3
votes
1answer
194 views

What is the use of declare with option -t

If you call the command help declare. You will see the following information: -t NAME : to make NAMEs have the `trace' attribute Is there any example that demonstrates the use of this option. I ...
1
vote
3answers
238 views

Why does a file move/copy function only move one file at a time when using the “*” wildcard?

function mv1 { mv -n "$1" "targetdir" -v |wc -l ;} mv1 *.png It does only move the first .png file it finds, not all of them. How can I make the command apply to all files that match the wildcards?
6
votes
4answers
478 views

Write a function that checks if a string starts with or contains something

I want to write a function that checks if a given variable, say, var, starts with any of the words in a given list of strings. This list won't change. To instantiate, let's pretend that I want to ...
0
votes
1answer
56 views

How to “export” functions in bash?

I have a script, which has a lot of functions inside. myfile1.sh: func1() {} func2() {} I can call them from script with: myfile2.sh: source myfile1.sh func1 func2 But how can I call them if ...
1
vote
1answer
34 views

Redirection to named file descriptor buggy inside functions

Bash has a feature where it will pick a file descriptor number for you and assign it to a variable. $ exec {fd}>foo.txt $ echo "$fd" 11 $ echo "bar" >&$fd $ cat foo.txt bar This feature ...
0
votes
0answers
27 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
20 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
51 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
97 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. ...
5
votes
3answers
462 views

What is “declare” in Bash?

After reading ilkkachu's answer to this question I learned on the existence of the declare (with argument -n) shell built in. help declare brings: Set variable values and attributes. Declare ...
2
votes
1answer
39 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
31 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
35 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
433 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
1k 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
184 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
47 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
112 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
80 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
90 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
46 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
87 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
45 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
613 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
817 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
65 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
324 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
42 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
67 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
212 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
70 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
165 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
123 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
88 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
71 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
144 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
263 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
464 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
72 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
72 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
7k 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
147 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
765 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
154 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
33 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
128 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='...