Skip to main content

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.

Filter by
Sorted by
Tagged with
2 votes
1 answer
102 views

Using https://stackoverflow.com/a/7948533/31298396, we can implement flags for a script testing as follows: #!/bin/bash # Flags # Source: https://stackoverflow.com/a/7948533/31298396 TEMP=$(getopt ...
Grass's user avatar
  • 145
3 votes
2 answers
149 views

POSIX defines shell functions as: fname ( ) compound-command [io-redirect ...] The compound-command is further defined as either: ( compound-list ) { compound-list ; } In particular POSIX notes for ...
Chris Davies's user avatar
0 votes
1 answer
69 views

I want to define a helper function in a zsh autoload-ed module. The problem is, if I just define in my my_autoloaded_fn file such as function helper { # ... } helper a helper b the name helper ...
Petr's user avatar
  • 1,776
2 votes
4 answers
203 views

I want to line up a couple of Bash functions based on the success or failure of the execution of the preceding function. If something fails, I want to print an error message: function_x has failed. ...
vrms's user avatar
  • 287
0 votes
0 answers
30 views

I've seen questions about this topic, but they talk about constructs like if or ||. I don't understand why the same behavior seem to happen for substitution ($())? $ my_function() { echo "the ...
Jakub Bochenski's user avatar
4 votes
1 answer
297 views

Lets define a shell function (here the shell is Bash) and test it $ s () { xterm -e sleep 5 & } $ s [1] 307926 $ [1]+ Done xterm -e sleep 5 $ With my specific meaning of ...
gboffi's user avatar
  • 1,406
7 votes
1 answer
609 views

I've read what's listed in Bibliography regarding unset, declare, local and "Shell Functions". My version of Bash is GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu) var='outer' ...
the_eraser's user avatar
2 votes
1 answer
225 views

I'm using alsa to fiddle around with the volume on a Bluetooth speaker I have connected. It's a 1-speaker setup; just something to provide some background. I can 'get' the volume setting fm the CLI as ...
Seamus's user avatar
  • 3,896
3 votes
1 answer
624 views

EDIT: sorry. this is my first question here. Here is a minimal working example #!/bin/bash # count=0 function something() { if test -f "$1"; then # is a file ((count++)) ...
Shlomo V's user avatar
-1 votes
1 answer
188 views

I'm trying to create a function to translate words via a custom script: function mean() { ~/scripts/translate.sh $1 } I would prefer the function to be named tr, as it is much shorter and faster ...
algonell's user avatar
3 votes
1 answer
347 views

When in bash some non existing command is run, corresponding error message appears: $ non-existent-command non-existent-command: command not found Is it possible to customize this behavior? I would ...
Anton Samokat's user avatar
3 votes
2 answers
400 views

I'm trying to set the fish history pager to be bat -l fish for syntax highlighting. (i.e. set the PAGER environment variable bat -l fish just for the history command). I tried: # 1: alias history &...
matan h's user avatar
  • 149
0 votes
2 answers
110 views

basic, innocent question: In bash scripting, why ever using a function, if one can set a variable containing command substitution with the essence of the function - a certain command or set of ...
futurewave's user avatar
2 votes
0 answers
48 views

Here is a simplified implementation of an issue in my bash/zsh aliases file: alias foobar='echo bar' # this is out of my control if true; then unalias foobar echo we got inside the conditional ...
Adam Katz's user avatar
  • 4,202
1 vote
1 answer
2k views

I have a Yunzii B705 keyboard. It has a row of multimedia keys that double as the function keys. Neither the multimedia nor function keys can work. I don't care about using the multimedia functions, ...
Village's user avatar
  • 4,277
0 votes
4 answers
1k views

Can somebody explain me, why the return value of a bash function is always echoed in the function and then consumed by my_var=$(my_func arg1 arg2 ..), when i look through code examples. my_func () {...
Schmaehgrunza's user avatar
3 votes
1 answer
115 views

I wrote a ksh function for git checkout (I've removed some irrelevant proprietary components for the sake of the public question, if you're wondering why it's useful to me): # Checkout quicker ...
Yehuda's user avatar
  • 351
1 vote
2 answers
148 views

Using zsh or bash, I want to run a script that may prompt the user for multiple commands and store them as a function, however I'm finding that eval "function $FNCNAME() {" or echo "...
Joakim Hagen's user avatar
1 vote
3 answers
536 views

I have a script like this that declares many functions which use another functions, etc.: #!/bin/bash function a { ... } function b { ... a ... } ... And another script that uses ...
k1r1t0's user avatar
  • 133
0 votes
1 answer
430 views

I'm trying to write a find and cd function like so: findcd () { cd "$(dirname "$(find '$1' -type '$2' -name '$3')")" } to be called like so: find . f [FILE_NAME] But it's ...
Mathew's user avatar
  • 243
1 vote
1 answer
294 views

I sometimes use characters such as ! and $ in commit messages, they need to be manually escaped, but not if you use single quotes like this git commit -m 'My $message here!'. I tried to write a ...
Justin Breen's user avatar
1 vote
2 answers
166 views

I'm trying to bowdlerize email addresses in a fixed length text file by generating a random string the same length as the input. I'm passing the string as a backreference in sed. To simplify, this ...
Paul Joiner's user avatar
0 votes
3 answers
1k views

I am working on a highly portable script that users shall source to their shells, forcing me to use POSIX scripting. There are many useful functions in the script, one of them is special though, as ...
Vlastimil Burián's user avatar
0 votes
1 answer
325 views

I'm trying to learn bash scripting using freeCodeCamp tutorial for beginners on YouTube. I'm stuck at the point where he shows how to create a function. He saved on a variable a command with an option ...
Paul Marcelin Bejan's user avatar
0 votes
1 answer
739 views

I have a bash script set up to monitor a number of UDP streams and convert it into actionable data. My problem is that I need to set the script to periodically check to see if the stream capture is ...
Dan D's user avatar
  • 3
2 votes
4 answers
357 views

A good and well-established guideline for commenting a shell function is to put it before the function's header. I am trying to follow these guidelines as much as I can, but this convention makes it ...
Rubem Pacelli's user avatar
0 votes
2 answers
245 views

Nesting problem with duck typing: function f1 { echo $1; } #with argument function f2 { N=$((0+1)); f$N "fff"; } #with dynamic name Desired result: function f2 { { echo $1; } "fff"...
mr.tee's user avatar
  • 119
0 votes
1 answer
146 views

I have bash function directory located at /git/function This is structure: /git/function/ ├── delete │ ├── delete.dir │ └── delete.file ├── main.sh ├── get │├── get.dir.sh │└── get.file.sh How to use ...
Rafal Niznik's user avatar
0 votes
0 answers
41 views

echo -n my_function() { echo -n "$@" } my_function hello world I get my desired output: hello world printf my_function() { printf "$@" } my_function hello world I get only ...
Bog's user avatar
  • 1,154
0 votes
2 answers
96 views

The behaviour of my shell environment changed: Earlier, when pasting a function definition e.g. function exampleFunc { echo hello } to the shell, it would display as formatted and register the ...
Lee's user avatar
  • 549
0 votes
1 answer
138 views

I'm attempting to write a function that writes arrays with a name that's passed in. Given the following bash function: function writeToArray { local name="$1" echo "$name" ...
Lee's user avatar
  • 549
2 votes
1 answer
385 views

Like HERE I have a file.csv with numbers in quotes: "0.2" "0.3339" "0.111111" To round the number (3 decimals) this solutions works great: printf "%.03f\n" $(...
R 9000's user avatar
  • 167
0 votes
2 answers
110 views

I have two files that both have the field "tumor id". Both are comma delimited. I need to add a field that is uniquely in file 2 to file 1, and I want to add the lines of the field according ...
Michelle Yang's user avatar
0 votes
1 answer
183 views

Is it possible to process a multiline variable in a bash function? Suppose I have a function theone and I pass a multiline variable to it. var=" This is a multiline variable" theone "...
Vera's user avatar
  • 1,373
0 votes
1 answer
9k views

This script, which i source in my terminal, offers the function _cphetzner, that is used to create a git repository. This function uses another function _feed_variable that prompts user for missing ...
MonkaS's user avatar
  • 1
0 votes
2 answers
64 views

I have this simple script, which does nothing more than: check if an email matches a specific pattern in that case, add a tag to a taglist before quitting, print that taglist set -e lista_tag=() ...
gobbolo22's user avatar
-1 votes
2 answers
4k views

I am getting confused about setting local variables in bash functions. It seems that using local dgt local ltr local braces local da could be safer than using local dgt ltr braces da I am ...
Vera's user avatar
  • 1,373
0 votes
1 answer
32 views

With the following function I want to be able to call it with nico-usage or with a numeric value to print a different string. Con this be cleaned up or made easier. nico-usage () { local ...
Vera's user avatar
  • 1,373
1 vote
1 answer
354 views

I'm writing a function, adding it to ~/.zshrc on my Mac. It's in order to more quickly handle commands to youtube-dl. I have this: function dlv() { cd /Users/admin/Downloads youtube-dl ...
Alfie Stoppani's user avatar
0 votes
2 answers
95 views

In the nano text editor, I can pipe the selection into a command, and I quite often need to center text, so I came up with the following code center() { str=$1 # Strip leading and trailing ...
user avatar
4 votes
1 answer
242 views

In Bash 5.2, the output of jobs after either of the following is identical modulo job numbers: sleep 3 # press C-z s() { sleep 3; } s # press C-z In both, jobs produces something like [1]+ Stopped ...
D. Ben Knoble's user avatar
1 vote
0 answers
112 views

I have a number of bash functions which I source from my .bashrc. I also have a bunch of support function that are needed but do not want them to show up in a terminal session when a user does tab-...
Vera's user avatar
  • 1,373
0 votes
2 answers
309 views

I am attempting to write a function which will take two commands as inputs, time the executions of both of them, then output those times to a text file. After reading this post, I got most of the way ...
David Robie's user avatar
1 vote
0 answers
101 views

For context, my file/directory structure and contents are mostly copied from how it is in Luke Smith's dotfiles (voidrice). Also, the "..." lines in the code blocks aren't code and represent ...
zxcdv's user avatar
  • 11
0 votes
0 answers
10 views

I have wrote the simple script #!/bin/bash progname=$1 sed -i.bak 's/#define SFFX "_.*"/#define SFFX "_$progname"/' path/to/file In order to find a preprocessor directive in a C ...
opisthofulax's user avatar
1 vote
1 answer
168 views

TL:DR How do I add a grep to a bash function while allowing a variable number of optional inputs? I find that in repeated grepping of large outputs, I end up clogging the terminal with data. ...
David Robie's user avatar
1 vote
2 answers
2k views

For context, I'm using zsh. Every time I use locate, I want to pass the -i and -A flags. Usually, if I can get away with it, I create an alias with the same name as the existing command to do this. ...
Daniel Kaplan's user avatar
5 votes
4 answers
822 views

Based on this answer, to measure the time in my bash scripts I can use: start_time="$(date -u +%s.%N)" sleep 5 end_time="$(date -u +%s.%N)" elapsed="$(bc <<<"$...
Saeed Neamati's user avatar
1 vote
2 answers
609 views

Using zsh, I'd like to create an alias or a shell function that operates as follows: I want this alias or shell function to echo its command line without honoring any shell control characters such as &...
HippoMan's user avatar
  • 847
0 votes
2 answers
150 views

I have the following bash function that returns 0 when tho variable verbos is defined. Have read the bash manual which says that when return command return N, the N is omitted, the return status is ...
Vera's user avatar
  • 1,373

1
2 3 4 5
12