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.

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 ...
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 $? } ...
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 ...
4
votes
6answers
2k views

How to export all Bash functions in a file in one line?

In Ubuntu 16.04 I have a Bash file containing a few different functions for automating various common tasks on my system. I have sourced that file in bashrc so I could comfortably call each function ...
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 ...
2
votes
3answers
387 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 ...
2
votes
2answers
38 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 ...
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 ...
2
votes
3answers
177 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. ...
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 ...
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]}" )" &...
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....
2
votes
4answers
181 views

Run a specific function from a file that contains 2 or more functions, in a detached tmux session

I use Ubuntu 16.04 with Bash and I have a file that contains many different functions. In between these functions, there is this one: tdm() { chmod -R a-x,a=rX,u+w "$drt"/phpmyadmin/ sleep 2h ...
2
votes
1answer
98 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 ...
1
vote
2answers
81 views

How to make a customized function in bash file

I am looking for a way to create a customized function in one of the setting files, so that when I open up a new session, the same customized function can be evaluated (or sourced), and I can use the ...
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
352 views

How can I convert a folder of audio files into a single file (iterate over many folders)?

I have a couple of radio plays of various series. Some are already single-track, some are multi-track. I want all to be single-track. I don't mind re-encoding; in fact I want to transfer them to my ...
1
vote
1answer
1k views

`set -e` inside a bash function

Does set -e behave differently here set -e; function foo { } vs. function foo { set -e; } does set -e belong inside functions? Does set -e declared outside of functions, affect "nested" ...
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 "...
1
vote
1answer
137 views

Bash function that creates an alias gets called endlessly

I ran the following Bash function that adds a string with expanded variables, into the end of my bashrc: alias() { echo "alias $repo=\"$HOME\"/$repo/$repo.sh" >> "$HOME"/.bashrc source "...
1
vote
1answer
28 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
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 │ │ ├──...
1
vote
2answers
100 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
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-...
1
vote
1answer
567 views

BASH: declare a local function in remote host with interactive shell

glad to write it for you here. I've done a lot of searching about that and haven't found a solution. What I want to do is relatively simple. There is a caveat local bash function in my workstation ...
1
vote
1answer
1k views

Start function in background and save PID

I have a script containing two functions. one function starts the other as a background process and returns that processes UID. This works well, as long as I don't want to put the PID into a dedicated ...
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 ...
1
vote
0answers
45 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 ...
1
vote
0answers
81 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
0answers
71 views

Would a 'function_not_found_handle ()' loadable bash builtin be possible?

I wrote a function to handle unknown commands in bash via command_not_found_handle (). My intention is to source scripts on demand and access its functions inside the handler. But unfortunately the ...
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/...
0
votes
2answers
118 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
2answers
41 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
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
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 ...
0
votes
1answer
153 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" && ...
0
votes
2answers
70 views

Bash: A function-like structure that doesn't require a call? [closed]

I desire some kind of data structure that looks similarly to a Bash function but isn't a function. A named structure to organize commands by context, that should also allow leading tabs. This is nice ...
0
votes
1answer
56 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
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
1answer
57 views

Bash function call from script file fails, while call from terminal succeeds

In Ubuntu 16.04 with Bash I created a file $HOME/ulcwe/software_internal.sh that contains a function rse() without a call. The file is sourced in ~/.bashrc (which was sourced by itself) and I can can ...
0
votes
2answers
78 views

Find location of shell / bash function on disk [duplicate]

If I have a function like so: #!/usr/bin/env bash function foo { } and the above function is in .bash_profile, or somewhere. If I use which foo it won't give me the location of this ...
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
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 ...
0
votes
1answer
48 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 ...
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 ...
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
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
39 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 ...
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 ...