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
3 votes
2 answers
296 views

how to alias the `history` function in fish shell

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
  • 147
0 votes
0 answers
28 views

eLisp: how to write a correct remove-number-grouping function?

In EmacsWiki, sub page "Add Commas To Numbers", it says: Here is how to transform a number such as 12345 into the string “12,345”. (defun add-number-grouping (number &optional separator)...
futurewave's user avatar
0 votes
2 answers
57 views

Bash scripting: When to use variable, when function?

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

Can't unalias then redefine as a function in the same conditional [duplicate]

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,012
1 vote
1 answer
151 views

How to get function keys working on a Yunzii keyboard?

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
  • 3,717
0 votes
4 answers
101 views

return value of a function

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

How does my function break `functions`?

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
  • 301
1 vote
2 answers
89 views

Start interactive function definition from shell-script

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

In bash shell on CentOS, what's the proper way to define a function in my ~/.bash_profile file?

I'm using bash shell on CentOS. I would like to define a function that runs a PostGres 14 query and outputs the result. eth_price 2023-12-06 10:05 In my ~/.bash_profile file, I defined eth_price() { ...
Dave's user avatar
  • 2,608
1 vote
3 answers
146 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
132 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
  • 243
1 vote
1 answer
179 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
94 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
471 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
147 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
348 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
289 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
158 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
108 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
34 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 ...
Bog's user avatar
  • 1,034
0 votes
2 answers
61 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
  • 519
0 votes
1 answer
61 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
  • 519
2 votes
1 answer
311 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
85 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
117 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
0 votes
1 answer
2k 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
50 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
485 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
0 votes
1 answer
29 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
1 vote
1 answer
268 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
51 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
4 votes
0 answers
149 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
86 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
0 votes
2 answers
202 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
85 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
116 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
926 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
782 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
397 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
  • 657
0 votes
2 answers
113 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
0 votes
1 answer
147 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
720 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
1k 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
  • 596
0 votes
0 answers
486 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,951
2 votes
1 answer
244 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,951
2 votes
1 answer
467 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
160 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
389 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
192 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

1
2 3 4 5
12