Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [bash-functions]

The tag has no usage guidance.

2
votes
2answers
39 views

How to squelch sh errors from imported bash functions?

So I've got a .bash_aliases file with many exported bash functions inside; a good deal of which are incompatible with sh... The problem is that sh is used all over my laptop (OS X) and linux machines ...
0
votes
1answer
57 views

How to “export” functions in bash?

I have a script, which has a lot of functions inside. myfile1.sh: func1() {} func2() {} I can call them from script with: myfile2.sh: source myfile1.sh func1 func2 But how can I call them if ...
0
votes
1answer
49 views

Passing parameters to a BASH function

I'm trying to run FoxitReader from the command line. For opening it, I created this function: foxit(){ /home/quora/opt/foxitsoftware/foxitreader/FoxitReader.sh } export -f foxit And put it ...
1
vote
1answer
20 views

MacOS: Changing screen capture location

I make a lot of presentations that involve many screenshots, and I want an easier way to organize them by project. I'm trying to write a simple function that changes the location where screenshots are ...
0
votes
0answers
9 views

associative array scoping issue with async function in bash script [duplicate]

Okay, I ran into a scoping issue using a function to update an associative array in bash. I want to run the function on parallel threads and store the value in the array to later iterate through. But ...
0
votes
1answer
37 views

Change value from file inside a while

I have a file like a database where I save name,surname,date of birth, gift, and I want to know if this person have got a gift from us or not. Those dates are splited by tabs in the file, one person ...
0
votes
2answers
42 views

How can i echo the value of [ 4 -gt 3 ] ,that is a test expression in bash?

How can I echo the value of [ 5 -gt 4 ] ,that is a test expression in bash? [ 5 -gt 4 ] | echo and echo `[ 5 -gt 4 ]` both end up printing a blank line @Thomas Dickey 's answer works but could ...
0
votes
2answers
124 views

Bash not appending array to new index. Just extends first index

I am using arrays in bash and one particular array is behaving unusually. I am using a function and calling an external script which returns a value to be appended to an array as follows: function ...
0
votes
1answer
43 views

git pre-commit script

The goal is to exit from the script with a non-zero exit code when committing package-lock.json with no associated changes to package.json being committed. #!/bin/bash # exits with 1 if there were ...
2
votes
3answers
188 views

How to signal a trap in “global” bash scope from function?

I want to execute a trap command in "global" scope, but the signal will come from within function. Of course it is possible to declare the variable globally beforehand or use the -g declare option. ...
-1
votes
1answer
89 views

Why would calling cd (builtin) from cd (shell function) without “command” cause an endless loop?

I once read in a Bash book by O'Reilly the following (my summary): command and enable cd () { echo "Improved CD !" # Improvement one; command cd # Improvement two; # Do cool ...
0
votes
1answer
55 views

Target system bash give me parameter error when I use `local -n`. How am I allowed to solve?

I have a script that contains a clearCnt function that works correctly on my Linux system with bash version GNU bash, version 4.3.48, but doesn't works correctly on the Linux target system. The bash ( ...
0
votes
1answer
54 views

How am I allowed to pass a void parameter through bash functions?

I'm writing something like the following script. It's doesn't work as expected because the function deleteCEfromCeLst has to call a script specifying to it a "void" parameter through the spawnPrg ...
0
votes
1answer
54 views

Output string to log file, then pipe to function as argument

So I am trying to echo a string out to a log file and also pass that echo'd string over to a function along with other arguments as well. I can't seem to figure out how to do it or find any answers on ...
1
vote
1answer
29 views

cycle through reverse search in a custom command history file in bash

I added a line to ~/.bashrc file which appends all issued commands to ~/.bash_eternal_history file in a date directory history-number command format, such as: 2019-01-08.23:59:32 /home/sport/...
1
vote
0answers
46 views

Define function without name and immediately call it [duplicate]

I have a script file (it's .bashrc actually), in this file I have a lot of variables and functions which are used only inside this script, but these variables and functions are publicly visible for ...
2
votes
1answer
99 views

Failing on errors inside a function which is called on the left-hand-side of an && expression

I am working on a Bash script with several functions. I would like to exit the script if any commands return an nonzero exit code, unless in a context where that code is being explicitly handled (such ...
0
votes
1answer
67 views

What is the best way for storing and versioning his aliases/functions/symlinks?

actually I have these lines (exemplary) in my .zshrc file: . ${TOOLS_HOME}/cli/.cli_base . ${TOOLS_HOME}/cli/.cli_functions . ${TOOLS_HOME}/cli/.cli_symlinks This works fine so far. My only problem ...
0
votes
1answer
40 views

How can I extract a function from bash/zsh with all its shell-related dependencies?

I write a lot of little useful functions, and sometimes I like to share them with friends or the world. The problem is that I use a lot of my helper functions and aliases in my functions, and I don't ...
2
votes
1answer
91 views

How may I echo all but the last parameter in bash?

I have the following #!/bin/bash function f1 () { echo "${@:1:-2}" } f1 1 2 3 4 5 6 I need to echo 1 2 3 4 5 man bash tells me that when I use @ I can't use a negative length. I resorted to using ...
2
votes
1answer
19 views

How to reference an array with a function prior to bash4.3

If we: Define an array; and then.. Define a function; and want to.. Call that array from inside the function.. We can. Like so: Input: myArray=('1' '2' '3' '4' '5') myFunction () { local -n ...
0
votes
3answers
96 views

Duplicate elements of an array except the first and last elements

I have an array, and I would like to repeat each element except the first and last elements. For example if the array has five elements 1 2 3 4 5, then after repeating, its elements should be 1 2 2 3 ...
1
vote
1answer
47 views

How to see which files are identical given a list of files in two projects

I have two projects named project1 and project2 with identical file trees as follows. project1/, project2/ . ├── src │ ├── index.html │ ├── main.js │ ├── normalize.js │ ├── routes │ │ ├──...
0
votes
2answers
49 views

how to write some text into a file in different machine

I have 2 machine machine1 and machine2. I want to write from write some contents into file in machine2 from machine1. In a same machine I can run sudo cat ~/.ssh/id_dsa.pub >> ~/.ssh/...
-1
votes
2answers
310 views

Bash function with if then returns command not found

I'm trying to write a function that does this: $ test-function -u {local-path} {destination} If the first parameter is -u, then execute a function that takes the next 2 parameters as the path of the ...
0
votes
3answers
69 views

Patching a bash function

I have a shell function (ok, it's the deactivate() function defined by a Python virtualenv), and I would like to patch it by inserting some statements before or after it's executed. My goal is to have ...
0
votes
1answer
72 views

Can an array be the first argument to a function? [duplicate]

I have a function that one of the arguments is an string that has space separated words. E.g. "foo bar ccc" I think this kind of string can be treated as an "array" and loop over each word. My ...
16
votes
1answer
2k views

Bash script function names containing double colon '::'

I came across a Bash script today that has function names with double colons :: in them, e.g., file::write() and file::read(). I've never seen this syntax before in a Bash script, and when I invoked ...
0
votes
0answers
35 views

why bash prompt won't update unless I call bash again [duplicate]

I have this function that echoes the recent directory funtion prompt_oldpwd() { local oldpwd=""; # check if the $OLDPWD is set and not empty if [ ! -z "${OLDPWD}" ]; then # ...
0
votes
1answer
156 views

Role of export -f statement when creating functions in bash_profile

I came across a bash_profile file that makes use of the export -f statement in the following manner: # Run xelatex and BibTeX function xelatex_bibtex { xelatex -shell-escape "${1}.tex" && ...
2
votes
3answers
390 views

grep with heredoc in function

I want to make function that parses from text pasted in a terminal. $ cat <<eof | grep --color sometext > foo > sometext > sometext1 > a sometext > asdf > > eof sometext ...
0
votes
1answer
33 views

Using Multiple Function to get an output in a single Line

I am trying to create output which keeps hostname,ssh protocol and root login information with bash script. And I would like to do it with developing functions. I developed .sh but it does not work. ...
0
votes
1answer
282 views

Bash the function is executed twice

The script has traps that execute a function that reruns the host script, but executes it twice. #!/bin/bash LOCK="/tmp/performanceScript.lock" exec_func () { #Creando Script de Vida Eterna. rm -f ...
2
votes
1answer
1k views

Bash return an associative array from a function and then pass that associative array to other functions [closed]

I'm trying to build an associative array in a function from a list pass in via an arg, but It just isn't working: #!/usr/bin/env bash function cwd { echo "$( cd "$( dirname "${BASH_SOURCE[0]}" )" &...
0
votes
2answers
54 views

Build a time elapse function [closed]

i want to build a time elapse function inside a script to print that time every second for example the output should look like below: finding files inside a directory time elapse HH:MM:SS HH is hour ...
3
votes
0answers
54 views

Git - prune every whitespace-separated word originally introduced by specific author in project's history

We have a project under git revision control with only a single branch. We need to remove every new whitespace-separated word that was introduced for the first ever time in a given file by a specific ...
1
vote
2answers
101 views

Creating contains function for a Bash array

I have this contains function which is supposed to check if an array has a certain value. The array itself is passed as the 1st arg, and the value is the 2nd arg. #!/usr/bin/env bash set -e; ...
-1
votes
1answer
62 views

Bash 3 compatible code [closed]

I have this bash function: zmx () { "$@" 2> >( while read line; do echo -e "r2g: $line"; done ) > \ >( while read line; do echo -e "r2g: $line"; done ) } what it does is append "...
0
votes
1answer
45 views

sourcing bash script does not acquire function def

This is a weird one - it should work. I have this file /home/foo/waldo.sh #!/usr/bin/env bash waldo(){ if [[ -z $(command -v waldo) ]]; then npm install -g '@oresoftware/waldo' || { ...
0
votes
0answers
107 views

bash function with dot in the name [duplicate]

I have this: docker.r2g(){ echo "foo"; } export -f docker.r2g my editor complains, I try these instead export -f 'docker.r2g' export -f "docker.r2g" I get this message: 'docker.r2g' is not a ...
2
votes
1answer
75 views

Return last command executed in bash function

Similar to this: Return last command executed in shell-script. lastCommand=$(some command here) Is it possible to achieve same result, but instead used inside a bash function, not a bash script? P....
6
votes
1answer
412 views

Why does declare -F give the wrong file

I have 2 small functions to abbreviate set -x / set +x, namely: sx () { export PS4='+ [${BASH_SOURCE##*/}:${LINENO}]' set -x } and sz () { set +x export PS4= } These 2 functions ...
0
votes
2answers
124 views

How to rename multiple files in Bash Scripting? [closed]

Suppose i want to rename 3 files in the same directory. Example: test1.gzip test2.gzip test3.gzip Now want to rename all the above files to something like, test1_20180518.gzip test2_20180518.gzip ...
8
votes
4answers
4k views

Implicit return in bash functions?

Say I have a bash function like so: gmx(){ echo "foo"; } will this function implicitly return the exit value of the echo command, or is using return necessary? gmx(){ echo "foo"; return $? } ...
1
vote
0answers
82 views

Simple function with `pbcopy` hangs [closed]

I'm trying to write a function that copies STDIN to the clipboard using pbcopy but also removes trailing newlines: cbc () { echo -n "${1}" | pbcopy } But when I run cbc $PWD the process just ...
1
vote
1answer
47 views

function and pass arguments

So this is my first function: #!/bin/bash lets_print () { echo hello $1 } lets_print Earth lets_print Mars ready to show result user@bash$ ./demo.sh Hello Earth Hello Mars And this is my ...
1
vote
1answer
125 views

Hyphen (-) ignored in the end of a piped command nested inside a function?

I use Ubuntu 16.04 (xenial) with Bash 4.3.48(1). Based on the answer of Stephen Kitt in this session, I tried run the following command as part of a function named imb(): mysqldump -u root -p --all-...
2
votes
2answers
50 views

File creation won't take place from within a function in a sourced file

In Ubuntu 16.04 xenial with Bash 4.3.48 and LEMP, I've created a comfortability function to create a php info file (to better know my php environment), and then delete it after 2 hours. but I'm having ...
1
vote
1answer
29 views

Adding debug output scrabbles result from function

I have a function that returns a value. But I would like to add some echos for easier debugging. But this scrabbles the result. Example: function foo { echo "In foo" VAR="1234" echo "...
-2
votes
1answer
66 views

How to call user defined function from another user defined function in bash?

I want to do this -- function swap(){ mv $1 $1._tmp && mv $2 $1 && mv $1._tmp $2; } function alterpdf(){ swap(titlepage_$1.tex titlepage_$2.tex); swap(titlepage_$1.pdf,titlepage_$2....