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
1 vote
3 answers
76 views

Run nested local bash functions via ssh

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

how to write function with nested commands [duplicate]

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
  • 235
1 vote
1 answer
107 views

Run `git commit -m` with single quotes in zsh

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

String Length Always Returns 2 in ZSH Function

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

Does `return 0` equal `true` (in sourced script to shell's environment)?

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

How to store command with option on variable of a function in zsh?

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

Running a function as process with a set process name or id

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

Function's comment is not printed out when putting it before the function's header

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

How we can recursively expand inline functions and aliases inside function?

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

BASH: use . instead of a space for command completion

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

printf and echo -n Work Differently - Why? [duplicate]

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 ...
Pixelbog's user avatar
  • 661
0 votes
2 answers
56 views

> symbol appearing when interactively defining function in bash

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
  • 491
0 votes
1 answer
56 views

Bash create parameter named array within function

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
  • 491
0 votes
0 answers
122 views

How to change the default actions for the function keys?

Most computers these days comes with function keys which already have some action setup for it. For eg; Fn+F1 will mute the device and triggers a XF86AudioMute. How can I change this triggered action? ...
an4s911's user avatar
  • 131
2 votes
1 answer
234 views

printf "%.3f" ${variable with newlines} - error with \n

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

How to add corresponding strings to a file from another file that it shares a field with?

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

Processing a multiline variable line by line within a bash function

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,183
0 votes
1 answer
1k views

How to return from bash functions the right way?

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

How to pass commands around?

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

Local variables in bash functions

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,183
0 votes
1 answer
28 views

function that allows different outputs dependent on argument values

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,183
1 vote
1 answer
192 views

How to write a function that takes an argument string that does not need to be quoted?

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

Shell function to center its first argument

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
3 votes
0 answers
122 views

Why do backgrounded commands in Zsh functions not show correctly in jobs?

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

Avoiding support bash functions to show up in terminal sessions when tab-completing

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,183
0 votes
2 answers
146 views

Compare Execution Times of Two Functions

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

startx function/alias has wrong arguments if not prefixed with exec

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

Linux regex in substitution of a string passing argument from CLI [duplicate]

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

Bash Function Grep Mod

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

How can I/Should I default flags when running a command?

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

How can I make time measurement DRY in my bash scripts?

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

zsh: alias or shell function to only echo its command line, including shell control characters

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
  • 565
0 votes
2 answers
100 views

Using bash return depending on return status of last command executed within a function

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,183
0 votes
1 answer
125 views

How to create functions using for loops?

I'm trying to use a for loop to create functions for choosing passwords in my Arch install script (based on easy-arch) using the following code (which uses this se answer for repeating prompts and ...
321ytrewq's user avatar
3 votes
4 answers
521 views

Bash function inside function: How to interpolate certain variables from outer function

I'm trying to create a function that creates a function. I want to "hardcode" an argument in the inner function based on what is passed into the outer function, e.g. outer() { inner() { ...
Andrew Cheong's user avatar
5 votes
1 answer
930 views

zsh: pass variable by reference, and modify its value (like bash's local -n namerefs)?

Is it possible to pass a variable to a zsh function, and modify its value without using eval? I came across a bash way to do that: local -n: https://stackoverflow.com/a/50281697
codepoet's user avatar
  • 576
0 votes
0 answers
394 views

how to run locally defined function on ssh remote-host?

I'm using bash, and below is my sample script. I've defined a function for remote ssh host, that runs another function. but when executed, it doesn't print anything. #!/usr/bin/env bash wdir=/var/www/...
Sollosa's user avatar
  • 1,899
2 votes
1 answer
228 views

BASH: is it possible to change a prompt of function based on its parameter?

I want to construct a function that will change its user input prompt based on its parameter. my_fucntion takes 1 parameter as db_host after prompting the user for input: function provide_host () { ...
Sollosa's user avatar
  • 1,899
2 votes
1 answer
341 views

How to preserve parameter expansion passed to a function?

I have this function: cyan=`tput setaf 6` reset=`tput sgr0` function Info() { echo "${cyan}$1${reset}" } And I use it in my other scripts as simple as Info some message. However, when I ...
Saeed Neamati's user avatar
1 vote
1 answer
153 views

zsh parameter substitution

This page has a demo function as follows: ~inews> namedir () { $1=$PWD ; : ~$1 } But when I try it on my machine, it produces the following error: namedir: no such file or directory: test=/home/...
In_Quisity's user avatar
0 votes
1 answer
351 views

GNU parallel export function output to variables failed

This script is to determine when destination file already exist, source file will update the destination one or be removed according to flag "$dup_act". #!/bin/bash dup_chk() { # $1: f_src,...
chuck lee's user avatar
0 votes
1 answer
163 views

How to Control Number of Parameters Entered to Function

I am trying to make a simple menu-driven calculator script. I want there to be an error message displayed whenever the user does not enter the correct parameters when they call the add() or subtract() ...
hashslingingbasher's user avatar
2 votes
2 answers
577 views

How to put command in terminal prompt without executing

I know you can simply put what you'd like to run: func() { cd scripts ./excellent/script } but, what I'd like to do instead is have ./excellent/script be put in my commandline without being ...
mcp's user avatar
  • 687
3 votes
1 answer
177 views

Making simple backup alias / function?

OK, so all I really want to do is backup single files as a time in the current directory with '.bak' extension. Here is my current function that does this (jsyk, using Rocky Linux): function backup { ...
GregTheHun's user avatar
0 votes
1 answer
122 views

What would be a typical usecase to use a lambda expression in Bash?

This is not a "problem to solve" question but rather a question about Bash didactics, literature and what's common between teachers. What would be a typical usecase to use an anonymous/...
Arcticooling's user avatar
0 votes
0 answers
357 views

Is valid use 'source' within a function?

Like an introduction: if secondary.sh has functionX(){ ... } and primary.sh has - source secondary.sh declared - and its own functions, therefore: # primary.sh content source secondary.sh ...
Manuel Jordan's user avatar
0 votes
0 answers
196 views

Passing one or more files in function in bash

Here I have written a sample bash script, the problem with this is that I am able to successfully pass file to function remFun but I am unable to pass the file unique.txt to function printfile. In ...
Tom's user avatar
  • 5
1 vote
1 answer
246 views

How to call many shell functions from sudo?

I have a huge alias file sourced by ~/.bashrc or ~/.zshrc which contains both simple aliases and more functions for more complex stuff: # example aliases alias "sudo=sudo " alias "...
Polizi8's user avatar
  • 175
2 votes
2 answers
496 views

Bash function to count the number of nested parentheses

I would like to create a bash function that, given a string, it count the depth level of nested parentheses, and return -1 if the parentheses are not balanced: function countNested(){ str="$1&...
Amazigh_05's user avatar
0 votes
1 answer
153 views

Calling a function from terminal without export from sources file [duplicate]

Have added a number of functions which I source from my .bashrc. For instance, I use export -f calc I also have another function usage_calc where I comment out the export call # export -f usage_calc ...
Vera's user avatar
  • 1,183

1
2 3 4 5
12