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
1 answer
44 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. ...
1 vote
2 answers
35 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. ...
0 votes
0 answers
24 views

Shell script with calling a FORTRAN program inside

I have a shell script that inside calls a function written in FORTRAN (its called distazim). I have used '!/bin/csh -x' to see where may be the error and it should be in the following line: ...
5 votes
4 answers
673 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 <<<"$...
1 vote
2 answers
43 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 &...
  • 385
0 votes
2 answers
43 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 ...
  • 111
0 votes
1 answer
42 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 ...
0 votes
0 answers
19 views

Can I attach code to vim exit function so that it would run whenever someone exists?

I want to be able to execute another Vim command whenever I close my vim window - i.e. I want that if I run :q, :q!, :qall, etc. I'll also run before that mks so that if this command was clicked by ...
3 votes
4 answers
234 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() { ...
5 votes
1 answer
125 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
0 votes
0 answers
57 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/...
  • 1,723
2 votes
1 answer
154 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 () { ...
  • 1,723
1 vote
1 answer
103 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 ...
1 vote
1 answer
92 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/...
0 votes
1 answer
109 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,...
0 votes
1 answer
46 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() ...
2 votes
2 answers
121 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 ...
2 votes
1 answer
70 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 { ...
0 votes
1 answer
68 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/...
0 votes
0 answers
70 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 ...
0 votes
0 answers
90 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 ...
  • 5
1 vote
1 answer
71 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 "...
  • 135
2 votes
2 answers
277 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&...
0 votes
1 answer
57 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 ...
-1 votes
1 answer
23 views

function validating numeric values used with conditional

I have a function that validates numeric values, and want to use it in if statements. num=3 if numeric -I "$num" then printf '%s\n' "$num is numeric integer" fi Is there a way ...
0 votes
1 answer
130 views

Is there a way to export functions with parameters already expanded?

Let's say I have two files main.sh and sub.sh in the same folder with the following contents: main.sh: #!/usr/bin/env bash export PARAMETER="main" my_func(){ echo "$PARAMETER $1"...
  • 3
0 votes
2 answers
316 views

Two different function prototypes for Linux kernel module ioctl

As pointed out in this question, the prototype for the ioctl function inside a Linux kernel module is: (version 1) int ioctl(struct inode *i, struct file *f, unsigned int cmd, unsigned long arg); or (...
  • 4,583
2 votes
0 answers
67 views

Why `declare -p VAR` returns "declare -- VAR" on an unset local variable but gives an error "bash: declare: A: not found" on an unset global variable?

I don't understand: $ declare -i VAR=0; \ > echo "$A"; \ > fun() { local -i VAR=1; echo {; echo "$VAR"; declare -p VAR; unset VAR; echo "$VAR"; declare -p VAR; echo ...
  • 244
0 votes
1 answer
67 views

Function tab-completion not matching that of wrapped command

I've got a function defined in my fish shell: function cl --wraps=cd cd $argv && ls -l --color=auto end According to man function, the --wraps option "causes the function to inherit ...
0 votes
2 answers
145 views

Use custom test function in bash conditional statement

I have a bash function called numeric that returns either 1 or 0. numeric () { # compute k either 1 or 0 echo "$k" } How can I use this function in a conditional statement to check if a ...
  • 31
1 vote
2 answers
74 views

When writing a bash script, how does the script know which usage() to call?

Let's say I write a bash function like so: function.sh usage () {echo "No arguments are needed";} myfunction () { if [[ $# -qt 0 ]] ; then usage fi echo "Hello World" } Then ...
  • 113
0 votes
2 answers
162 views

Finding duplicate aliases and functions in a script (.bashrc etc)

This site says that functions are faster than aliases, but he rightly points out that aliases are easier to understand - when you want something very simple and do not need to consider passing ...
  • 579
0 votes
1 answer
107 views

Use file marker with entr

I've the following code on myscript.sh for the purpose of execute function on a file with .md extension when this file is modified: function my_function(){ echo "I\'ve just seen $1" } ...
  • 3
0 votes
2 answers
90 views

bash, pass an argument to the 'history' command

I do the following to make history more sensible (i.e. seeing when a command is run can be fairly critical when troubleshooting) shopt -s histappend; # Append commands to the bash history (~/....
  • 579
0 votes
1 answer
161 views

How to evaluate bash alias before being passed to bash function?

I have defined a test alias as: alias testalias='python3 -c "f = open(\"/tmp/testopenfile\", \"w+\"); f.write(\"hi\n\")"' It works fine when I run it directly ...
  • 113
2 votes
0 answers
53 views

Problem with ksh namespaces defined inside functions

I'm new to ksh namespaces. The problem I'm seeing is that if I define a namespace inside a function, then variables defined within that namespace clobber the same-named variables outside the namespace ...
  • 21
1 vote
1 answer
103 views

How to parallelize a recursive bash function?

I have a bash function that mainly curls an endpoint for a set of links, and again curls each of those links (for another set of links) recursively. task() { link="$1" response=$(curl &...
0 votes
1 answer
63 views

files zipped using function not naming correctly

I'm probably missing something basic with bash function variable syntax here. My understanding of the zip command's syntax is that it's zip newfilename.zip filetobezipped. So I want my function to zip ...
  • 3
0 votes
1 answer
41 views

access name of unzipped file inside a function

I'm running Ubuntu on WSL2. I frequently download zipped homework files from my school's website. They go into my Downloads folder on Windows. I want them copied into a particular path on my Linux ...
  • 3
0 votes
1 answer
51 views

Copy files from multiple directories and paste it into multiple directories

I have a directory " /data/archive In this directory there are 8 different subdirectories. eg : /data/archive/a /data/archive/b and so on Files under these subdirectories have date appended to ...
0 votes
1 answer
641 views

Unable to run file from command line after adding to PATH

So I have a function that I wish to run from my command line. cat foo.sh #!/bin/bash echo foobar I export it to my PATH variable and change to a different directory. export PATH=${PATH}:/home/usr/...
  • 23
1 vote
2 answers
257 views

How to pass zsh alias function to pipe

I have a zsh alias: gitbs() { git branch | grep -- $1 } And I would like to pass the result into git checkout, for example: git checkout | gitbs state How can I make this work?
  • 13
1 vote
0 answers
38 views

zsh: Make getting the value of a variable invoke a function

I like to use a "getter" function for some variable foo, such that using "$foo" is equivalent to "$(foo-getter)". It think this should be possible, as $EPOCHREALTIME ...
  • 1,325
1 vote
1 answer
170 views

execute command on all visible shells

I'm currently running a tiling window manager and I want to be able to use a custom function that is equivalent to one I had when I was using tmux that allowed me to run a command in all visible ...
  • 341
1 vote
1 answer
196 views

Bash command to move and change current directory to it

I need a bash command to move a directory somewhere else and change the current directory to it. Something simple like mvcd I tried with mvcd () { mv "$1" "$2" cd "$2"...
  • 11
-1 votes
2 answers
419 views

Local variables in bash functions taking option arguments

Have been using local in my bash functions after matching argument options. ----- plist ----- local fdir=${dpath:-$PWD} pregion "${ropts[@]}" ----- pregion ----- ("-d"|"--...
  • 319
0 votes
2 answers
90 views

Bash function calling another function that requires passing user-defined options

I have a function plist that is able to call head and tail commands. But for processing regions I call a different function pregion. # --- plist --- ("-H"|"--head") local -...
  • 319
0 votes
1 answer
63 views

Passing file types to bash function and calling find

I have the following bash function to print between two line numbers, for files watching file types .texi and .org recursively in a particular directory. I would like to be able to supply the file ...
  • 319
1 vote
1 answer
350 views

printf format depending on number of parameters

I am writing a bash function that takes a number of strings, with each string to be printed it a separate line. But have to compose the frmt variable to printf appropriately depending on the number of ...
  • 319
0 votes
0 answers
56 views

grep on specific file types

I have the following bash function and want to pass my own file types to the function. I wonder how should I do it. greprc () { grep -hir --exclude=\*.el --include=\*.{org,texi} "$@&...
  • 319

1
2 3 4 5
11