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
39 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/...
user avatar
0 votes
1 answer
28 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,...
user avatar
0 votes
1 answer
37 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() ...
user avatar
2 votes
2 answers
41 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 ...
user avatar
2 votes
1 answer
42 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 { ...
user avatar
0 votes
1 answer
66 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/...
user avatar
0 votes
0 answers
51 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 ...
user avatar
0 votes
0 answers
38 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 ...
user avatar
  • 5
1 vote
1 answer
41 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 "...
user avatar
  • 135
2 votes
2 answers
221 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&...
user avatar
0 votes
1 answer
29 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 ...
user avatar
-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 ...
user avatar
0 votes
1 answer
53 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"...
user avatar
  • 3
0 votes
2 answers
180 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 (...
user avatar
  • 4,473
2 votes
0 answers
64 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 ...
user avatar
  • 234
0 votes
1 answer
27 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 ...
user avatar
0 votes
2 answers
73 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 ...
user avatar
  • 31
1 vote
2 answers
67 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 ...
user avatar
  • 113
0 votes
2 answers
100 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 ...
user avatar
  • 559
0 votes
1 answer
50 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" } ...
user avatar
  • 3
0 votes
2 answers
61 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 (~/....
user avatar
  • 559
0 votes
1 answer
94 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 ...
user avatar
  • 113
2 votes
0 answers
40 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 ...
user avatar
  • 21
1 vote
1 answer
84 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 &...
user avatar
0 votes
1 answer
34 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 ...
user avatar
  • 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 ...
user avatar
  • 3
0 votes
1 answer
35 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 ...
user avatar
0 votes
1 answer
195 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/...
user avatar
  • 23
1 vote
2 answers
166 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?
user avatar
  • 13
1 vote
0 answers
35 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 ...
user avatar
  • 1,287
1 vote
1 answer
69 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 ...
user avatar
  • 331
1 vote
1 answer
98 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"...
user avatar
  • 11
-1 votes
2 answers
128 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"|"--...
user avatar
  • 317
0 votes
2 answers
67 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 -...
user avatar
  • 317
0 votes
1 answer
48 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 ...
user avatar
  • 317
0 votes
1 answer
228 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 ...
user avatar
  • 317
0 votes
0 answers
36 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} "$@&...
user avatar
  • 317
1 vote
1 answer
101 views

Hyphen in bash function names [duplicate]

Am writing bash functions. It looks like function names can include hyphen: -. Thus indus-headrc () and indus-tailrc (). Had thought that only underscore is allowed.
user avatar
  • 317
1 vote
1 answer
39 views

Adding help option for function

I have the following function that print NUM lines from the beginning of a set of files. The command (function, script, whatever) accepts a variable number of arguments, and I would like to reserve ...
user avatar
  • 317
0 votes
0 answers
60 views

Script doesn't exit from function when send error report

when i launch my script, it still remains in process while function goes in error after send the mail: #!/bin/bash LOG_FILE=/data/logs/file.log DATE="$(date +%Y%m%d --date="1 day ago")&...
user avatar
0 votes
1 answer
37 views

In a bash/zsh function how do I do something, then depending on the output, do A or B?

This function looks through each local git repository in folder ~/src, and does git pull on it. More and more I keep getting an error Please commit your changes or stash them before you merge. ...
user avatar
3 votes
1 answer
332 views

Script function call: function vs $(function)

Taking how reference the following code for simplicity #!/bin/bash number=7 function doSomething() { number=8 } doSomething echo "$number" It prints 8. But with: #!/bin/bash number=7 ...
user avatar
-4 votes
1 answer
456 views

Use exit, but do not close terminal, in bash script

I have created a shell function to comfortably use desktop search tool baloosearch. I created it as a function for a reason. Now, in this functions I need to use exit command to quit loops, ifs or ...
user avatar
3 votes
2 answers
332 views

Parallelize a function using xargs and separate variables

I have a four line text file with 1, 2, 3 & 4 on each line. Also, I have a function that I want to parallelize: foo() { echo "$1 is not $2"; } I export the function: export -f foo Now I ...
user avatar
0 votes
1 answer
127 views

Is there a zsh command to output shell function code, like `type` in bash [duplicate]

In bash, when I'd like to have a glimpse at what an already defined shell function does, I can: $ type myFunctionName For a variable myFunctionName, it provides me with the type of the variable (a ...
user avatar
-1 votes
1 answer
104 views

Redirect a part of mapfile function to stdout but not into the array

Summary: Like we have Stdout Stderr, I would like to create Stdstatus. Stdout can be stored in array and Stdstatus can be printed for user. I didn't know that stderr could be used for other messages ...
user avatar
  • 1,362
-1 votes
1 answer
104 views

Declare and use ad-hoc function in bash in interactive shell

Basically something like // declare const my_func = function(param1, param2) { do_stuff(param1, param2) } // use my_func('a', 'b'); All in current interactive shell without using files
user avatar
  • 109
0 votes
1 answer
96 views

bash, "\" in an alias or function

I use Windows and Linux a lot, and sometimes I type cd\ from Windows muscle-memory, so I tried to alias that, alias cd\='cd /' but that doesn't work (presumably because \ is an the escape character in ...
user avatar
  • 559
0 votes
3 answers
106 views

Bash function seems to not want to correctly run lines

The question makes very little sense but lets get into it! I am working on a IRC Client in bash and I found some code and started adding it to an IRC bash bot code I have been working on. I have ...
user avatar
0 votes
1 answer
47 views

Where are Shell Functions held?

Thanks for any advice anyone can offer me on my question Is there's a hash table where defined shell functions are kept? If so, what is it called? So basically I'm wondering where in a shell (BASH) ...
user avatar
  • 121

1
2 3 4 5
11