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

call function by name with arguments

I'm quite new on the shell scripting front and was wondering whether it is is possible to call a function which itself than calls another function with none, one or multiple arguments. The first ...
1
vote
2answers
56 views

Correct way to take a function from the current shell and save it for future use?

I've been writing a lot of one-off functions recently. On the occasions that I go "hmm, I should save this" I use type <function name> to show the code, and copy and paste it into ....
1
vote
2answers
37 views

Executing user defined function in a find -exec call AND choosing version of that function based on arguments

This was my starting point: shell script - Executing user defined function in a find -exec call - Unix & Linux Stack Exchange But I need to choose between 2 different versions of the function, ...
1
vote
2answers
34 views

I want a function to ssh into a server and then run a list of commands

What I would like to do is: f(){ ssh myserver && ls && echo 'it works!' } However, when I run this function. Only the ssh is executed.
0
votes
1answer
64 views

How do I pass an array as an argument?

How do I pass an array to a function, especially if it is in the middle somewhere? Both "${b}" and "${b[@]}" seems to pass the first item only, so is there a way to both - call and ...
2
votes
1answer
54 views

bash: exported function not visible, but variables are

Over the years I've collected sort of a library of bash functions the shell and scripts refer to. To decrease the import boilerplate, I'm exploring options how to reasonably include the library in ...
-1
votes
2answers
47 views

How to create a main bash script that allows users to input which of 2 functions they want to run?

I currently have two bash functions, one which uploads, and another that downloads a file. I would like to create a bash script that allows users to specify which of the two they would like to do. The ...
0
votes
0answers
16 views

Run a set of functions in a detached screen

Is it possible to call a bunch of functions within a detached screen that are defined elsewhere in the script? For example: #!/bin/bash Method1() { ... } Method2() { ... } Method3() { ... } Method4() ...
0
votes
1answer
46 views

Why does make oldconfig not work with a bash function?

I have the following function in my bashrc: make() { nice -n 19 /usr/bin/make -j $(nproc) --output-sync "$@" echo -e "\a" } export -f make The purpose is to run make on ...
0
votes
1answer
35 views

How to call .bashrc function in script

~/.bashrc ---- example() { echo "example function" } If I use this in a shell script #!/bin/sh example I get command not found. I have tried #!/bin/sh source ~/.bashrc example but ...
0
votes
0answers
13 views

Local variables in zsh functions and their return code [duplicate]

Consider the following zsh function function foobar() { local foo=$(fzf) && echo "$foo-bar" } if instead of selecting one of the results of the fzf command I exit fzf without ...
0
votes
1answer
18 views

bash function to replace every occurence of text in directory and subdirectories

I found (on Google) this perfectly working line to replace every occurences in all files in my directory and subdirectories: grep -lr previoustext | xargs sed -i 's/previoustext/newtext/g' It works ...
0
votes
0answers
20 views

ZSH's function with parameter not working as expected

I have created a function in .zshrc: function gc { git commit -m $1 } but when I am trying to call this function: gc "some commit message" I get: error: pathspec 'some commit message' ...
1
vote
1answer
58 views

appending values in a dynamic array

#!/bin/bash range=$(seq -f "cen%04g" 1052 1099) range1=$(seq -f "rh%04g" 1052 1099) check () { for node1 in ${range};do ping -q -c 1 -w 3 -s 10 $node1 >/dev/null if [ ...
7
votes
3answers
828 views

How to undefine a zsh command created accidentally?

I wanted to search for the string '.vars()' in all my Python files, and somehow I redefined 'grep' as follows: % grep .vars() *.py % which grep ...
-1
votes
2answers
46 views

`declare -F` does not work in zsh

declare -f Shows function definition in both bash & zsh. $ declare -f VCS_INFO_adjust () { # undefined builtin autoload -XUz } VCS_INFO_bydir_detect () { # undefined builtin ...
1
vote
1answer
82 views

zsh does not reload functions in the prompt

I am trying to make the zsh prompt reload a function everytime a new prompt loads. The function outputs a version of pwd but shorter, if the output of pwd was ~/Downloads/Folder the function would ...
1
vote
1answer
26 views

receiving error 'script.sh: line 150: private: command not found'

I have this script: #!/bin/bash BASE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" > /dev/null 2>&1 && pwd )" USER_DEF=$(whoami) function private { ...
1
vote
2answers
40 views

Change shells in a script function

I'm rocking ZSH as my main shell, but in my .zshrc, I'd like to set up a ssh command with expect so I can ssh into my dev boxes easier when I flash builds (there's literally no security needed it's ...
-1
votes
1answer
39 views

How to add echo inside function

I am decently new to the terminal. I am currently trying to make a small function in my .bash_profile that would send a message to a specific contact from the terminal (without opening the Messages ...
1
vote
1answer
39 views

How can I use fork to calculate partial results of a calculation?

The task that has been given to me is that I have to find all numbers that divide a number (let's call it x) and find out how many times does it divide it. I have to use fork to do the task. What I ...
0
votes
1answer
18 views

Multiple values in variable and need to check IF condition for each value

I'm writing DB2 stored procedure. This stored procedure will be called using .ksh script. I was stuck in checking IF condition. VARIABLE = 'test1,test3,test8,......testn' The variable's datatype is ...
0
votes
2answers
37 views

Why are these particular semicolons necessary in this function definition and command-line?

I'm preparing for the LPIC1, exam 102. This question came my way and I absolutely blanked. I knew it when I first took the quiz, now a month and a half later it's all blurred in my head. What does ...
0
votes
1answer
21 views

Path dependent commands in .bashrc

I am looking for a way to simplify the different php versions and also paths of Composer. I found an approach in this answer that looks very appropriate for me. I have tried to implement the ...
0
votes
0answers
17 views

Alias or Function for a Command [duplicate]

Sometimes (or may be all the time) an Alias and Function for a command works the same way. For example, following Alias and Function of Ping Command: alias Pingoogle='ping -c 3 google.com && ...
0
votes
1answer
80 views

Passing a variable from one function to another in bash script

Iam new to linux and trying to pass a variable from one function to another in a same bash script. Below is my code: #!/bin/bash -x FILES=$(mktemp) FILESIZE=$(mktemp) command_to_get_files(){ aws ...
0
votes
1answer
33 views

How to execute command alias, saved as a string variable in a zsh function?

I have a function fz(){file=$(find | fzf) && echo $file && read order && $("$order" "$file")} in my .zshrz. It allows me to look up for a file anywhere in ...
1
vote
2answers
137 views

Got bad array subscript when wring a function printing the fibonacci numbers

This is my code to write a function which prints the Fibonacci numbers function fib { fib_array=( 0 1 ) count=3 while [[ $count -le $1 ]] do fib_new=$...
0
votes
1answer
74 views

Can I put $SECONDS into an environment variable in a bash script?

I'd like to be able to have $SECONDS shown with hours, minutes, seconds in an environment variable, so I only need to use, e.g. $RUNTIME in various places in the script rather than have the whole ...
0
votes
0answers
129 views

How to define global functions in zsh (macOS) that are available in scripts? [duplicate]

Since macOS 10.15 (Catalina) the default shell has changed from bash to zsh. One of the things I'm running into is I cannot get my own global functions working. I used to export these from ....
0
votes
2answers
32 views

Function can echo the value that has not received as input

I'm new to bash. I'm confused about the way functions work in this language. I have written this code: #!/usr/bin/env sh choice_func() { echo "$choice" } echo "Enter your choice:" read ...
0
votes
0answers
41 views

Calling a function with feh

I can call an external script with feh, like so: feh --action1 ';./renamepic.sh "$PWD/%F"' -F -Z "/Pictures/" But how do I call a function inside the same scrip, something under those lines: ...
0
votes
2answers
60 views

Bashrc function case statement to import cases from JSON

I'm making a .bashrc function to get me around the system faster. I used a case statement to state where I want to go. function da() { case "$1" in home) cd ~ ;; eolas) cd /...
1
vote
0answers
30 views

Variable not available outside function [duplicate]

I'm having difficulties understanding the following example I created. The variable glob_var gets changed inside the test() function, but the original value remains outside of the function: test() { ...
2
votes
2answers
293 views

How do I unset all functions in zsh?

I know that I can use unset -f $FUNCTION_NAME to unset a single function in bash / zsh, but how do I unset all functions?
1
vote
0answers
38 views

How do I write a function that has a double colon in a string without my current directory showing up in the string? [duplicate]

function awsl { echo "arg: $1" prfx='arn:aws:sts::' pstfx="$1:assumed-role/FOO/justin@bar.com" role="$prfx$pstfx" echo "role $role" saml2aws login --force --profile=default --role=$role } ...
-1
votes
1answer
23 views

Unable to pass an argument to a function

For my homework, I need to have a function in my code to earn extra credit. I have a script that takes the second argument and makes a folder from what is passed. The code works with the argument ...
1
vote
1answer
97 views

Bash, command as function is not working, but manually is OK

I need save mysql dump from remote machine to file on local, so connect via ssh to remote machine, run mysqldump on remote machine and save it to file on local, where script is running. I have 2 bash ...
0
votes
0answers
11 views

Function connect mysql and launch query

I've made a bash script for making an audit of a server, and I am trying to create a function with the following goal. The function get the mysql parameters (it's a pm file), store as variables and ...
0
votes
2answers
71 views

How to define the name of a bash function to be a regex

I've searched around and can't find an answer. I want to define a function like this function \d{2} () { echo $1_or_smth } Then if I say in my terminal 12 I want it to echo 12 back.
0
votes
1answer
30 views

Run a bash function that takes two files names as variables from command line

I'm running an Abaqus job in Ubuntu command line using two files (file1.inp and file2.f) as follows: abaqus job=file1 user=file2.f Since I'm doing this a lot with different files, I wanted to make ...
0
votes
0answers
30 views

Issue while fetching the records of each csv file in the loop

I have a requirement where I am facing some issues when i am trying to pass the argument to a function. Below is my requirement: I have four kinds of csv file which have a filename with keywords ...
0
votes
2answers
44 views

Bash: Run Command (with a specific pattern) only if another Command (with another specific pattern) has failed

I have a shell script that creates a function (in a bashscript file which is sourced by .bashrc) for each Markdown files to open it in the editor. Example: ls /home/nikhil/Notes/Studies Physics.md ...
3
votes
3answers
164 views

Find exec sh: Shell variable not getting passed to subshell

Here is a simplified code that prints the name of Directory if it contains a Filename with same name as the parent directory and .md extension. FIND(){ find . -type d -exec sh -c ' for d ...
0
votes
1answer
275 views

How to create a bash function from within a .envrc?

I want to use direnv to automatically define a bash function when I switch to a particular directory. Here is the function definition. seqchart () { # Create a sequence diagram creation shorthand ...
0
votes
1answer
120 views

How to pass a multiword string as argument in Linux [duplicate]

I am trying to pass a multiword argument in a function an echo the result in a simple fashion. Currently I am doing this: function myFunction { multiWordString="" for ((i=3; i<=$#; i++...
6
votes
2answers
1k views

zsh: `source` command doesn't reload functions

(This is on MacOS with zsh 5.7.1) Here is how I load custom functions in zsh: # Custom functions fpath=($HOME/.zfunc $fpath) autoload -Uz mackupbackup autoload -Uz tac autoload -Uz airplane autoload ...
0
votes
1answer
505 views

Random unbound variable error within function

I made a function in bash and when I call it, it crashes with an unbound variable error. I don't understand cause the variables that are said to be unbound are declared. Moreover, it seems to be ...
3
votes
1answer
140 views

Get and set the script arguments from within a function in bash

Context: I have an old bash script with a big section parsing its arguments. It happens now that I need to call this section twice, so I plan to move it to a function to avoid code duplication. ...
0
votes
0answers
37 views

make bash function context aware to list files from folder where it's called

As an alias can not be used to send parameters I have created a function in my .bashrc Just for the sake of the example calling chemacs script to keep different emacs distributions side by side. The ...

1
2 3 4 5
10