Stack Exchange Network

Stack Exchange network consists of 174 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.

0
votes
1answer
34 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
18 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
40 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
1answer
32 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
37 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
28 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
72 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
17 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
56 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
39 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
39 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
63 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
62 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
60 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 ...
8
votes
3answers
2k views

What is the difference between the terms “program”, “command” and “function” in Linux & Unix?

I would like to know whether the commands that we call in the shell are functions or programs?
15
votes
1answer
1k 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
31 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
89 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
293 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
29 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
138 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
399 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
49 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
46 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
67 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
50 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
38 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
52 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
48 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
384 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
44 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 ...
7
votes
4answers
2k 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
58 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
44 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
117 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
48 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
25 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
33 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....
4
votes
6answers
1k 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 ...
0
votes
1answer
47 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 ...
1
vote
1answer
89 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 "...
0
votes
0answers
23 views

Making a simple script for counting which members of a set have appeared, and outputting probability of appearance of the remaining members

I am creating a simple bash script, but am relatively new to it. Hear me out and the easy (I hope) question will follow. i need to make a script that will take input from the keyboard to know which ...
1
vote
1answer
186 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 ...
2
votes
4answers
160 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 ...
0
votes
2answers
69 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 ...
1
vote
2answers
77 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
0answers
52 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
4k views

How to read the output of a previous command in a shell script? [duplicate]

I executed a command in a shell script and the command ends with "completed successfully" or "completed unsuccessfully". How can I read that in the script and use it for if and else conditions?
1
vote
1answer
770 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
378 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 ...