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
votes
1answer
32 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 ...
-1
votes
1answer
29 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
-1
votes
0answers
38 views

Create OpenVPN Alias

The goal is: create an alias that points to the folder that contains the .ovpn files and that already inserts this command "sudo openvpn" and then I just add the name of the file I want to ...
0
votes
1answer
24 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 ...
0
votes
3answers
77 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 ...
0
votes
0answers
29 views

php call multple mysql values using the same formatting code

I'd like use php to to recall multiple mysql values keeping the same formatting code, sometihng like <h2>[genre]</h2> <h3>[title]</h3> <p>[contents]</p> With many &...
0
votes
1answer
40 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) ...
0
votes
2answers
64 views

continue: only meaningful in a `for', `while', or `until' loop

I have a loop that checks for certain criteria for whether or not to skip to the next iteration (A). I realized that if I invoke a function (skip) that calls continue, it is as if it is called in a ...
1
vote
1answer
41 views

How to display only shell variables (not functions) [duplicate]

How can I display a list of shell variables in Bash, not including functions, which often clutter the output, because of the many related to completion? I have examined declare, and it has an option ...
6
votes
1answer
746 views

How to make a multiline alias in Bash?

I want to make an alias for a multiline ​command to call it faster then copying-pasting-executing it from a text file each time. An example for such command is this execute-a-remote-updater command: ( ...
0
votes
0answers
27 views

Shell Script to send mail using function Definition

I am new to shell script and trying to run the script which will run a command and redirect the output to a log file and then mail that log file. where I will be repeating this step many times and do ...
0
votes
1answer
19 views

Execute function in bash on every keystroke in interactive shell

I would like to run a function every time a user presses a key in an interactive bash shell. I am wondering if bash has anything built in like the self-insert function in zsh? Alternatively, I am ...
3
votes
2answers
990 views

How to pass all arguments of a function along to another command?

Hello I have this in my ~/.bash_profile export GOPATH="$HOME/go_projects" export GOBIN="$GOPATH/bin" program(){ $GOBIN/program $1 } so I'm able to do program "-p ...
0
votes
0answers
16 views

assign to a number named variable to mimic command line argument [duplicate]

I have a script where I am passing commands to processes with an arguments passed from command line and from environment variables as well. To simplify my use case lets assume my processes are echo ...
1
vote
1answer
52 views

zsh - How do I make local variables available to inner functions that are not defined with 'eval'?

I have a zsh function that looks like this: outer_func () { local LOCAL_VAR='test' inner_func () { echo "${LOCAL_VAR}" } } where $LOCAL_VAR is not available to ...
-1
votes
1answer
32 views

python: self.variable within function [closed]

I am dealing with tutorial Python code focusing on Classes and its functions. I am wondering about self. method for the variables defined inside the class. Should I use self. only for the variable of ...
1
vote
1answer
27 views

Return from higher function, how - if possible?

I have a somewhat difficult time figuring out how - if possible - to return from a higher function, let me show you a POSIX code tidbit: sudoedit_err () { printf >&2 'Error in sudoedit_run()...
-2
votes
1answer
83 views

How can I exit a function, without exiting the file [closed]

Hi i am creating a subshell in Unix with functions, but when i execute a function it finishes the process. I want to know how can I exit a function without exiting the subshell.
0
votes
1answer
63 views

bash: functions containing part of AWK code

within of my bash code I have a part of an sed + AWK code, which do interatively some operation on input file and add the results to another txt file (the both filles had been created by the same bash ...
1
vote
1answer
58 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
65 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
70 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
40 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
130 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
134 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
67 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
17 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
80 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
133 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
14 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
21 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
33 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
61 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
912 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
89 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
343 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
59 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
41 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
78 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
116 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
26 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
41 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
158 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
62 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
302 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
142 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
498 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
37 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 ...

1
2 3 4 5
10