The tag has no wiki summary.

learn more… | top users | synonyms

10
votes
5answers
261 views

Stateful bash function

I’d like to implement a function in Bash which increases (and returns) a count with every call. Unfortunately this seems non-trivial since I’m invoking the function inside a subshell and it ...
1
vote
2answers
10 views

Install-on-Demand Wrapper Function for Executables

How do I make the following function work correctly # Install git on demand function git() { if ! type git &> /dev/null; then sudo $APT install git; fi git $*; } by making git $* ...
2
votes
1answer
20 views

Expand variable in function definition in zsh

I want to generate a set of functions in my shell in a for loop, but I can't see how to access a variable inside the function body of the function I'm creating. In essence, I would like the following ...
2
votes
3answers
41 views

how to prevent alias expansion by `eval` to an arbitrary alias, and keep the endless loop protection on a function?

IMPORTANT: do not use eval! (I learned this later..) In a function, eval expands sleep to its alias, so I prevent the endless loop this way: function FUNCexecEcho() { echo "EXEC: $@"; shopt -u ...
1
vote
2answers
49 views

Nature of the positional parameters

I am beginner to shell scripting and wrote this script which sort of confused me a bit... function func { the variables received are echo $0: $1 and $2 } echo in the main script func ball boy The ...
2
votes
3answers
813 views

How to add a function to .bash_profile/.profile/bashrc in shell?

I have a function which converts epoch time to date. Here is the definition date1(){ date -d @$1 } I'd like to be able to write: $ data1 xxxyyy And I want the corresponding date. I understand ...
1
vote
1answer
176 views

How to export a function in bash over ssh?

I would like to write a function in bash, then export that function and execute it over ssh. Is that possible, and if yes, how? I tried #!/bin/bash function myfunc() { echo $1 } export ...
0
votes
1answer
27 views

Using variable in KSH function

I tried to have a switch if either an option is set or not while getopts "s:u:d:e:ch" _OPTION; do case $_OPTION in ... c) isCSet="Y" then I'm ...
0
votes
1answer
51 views

Function, return value using pwd in KSH

I tried to write a small ksh script: fDestExists (){ cd /tmp read vANSWER?" >> Do you want to create a repository in pwd ? Type YES or NO" echo " |----> $(fGetDatum) You ...
1
vote
1answer
134 views

getopts - how to avoid read next $OPTARG as argument?

Just started use getopts and found one problem... For example - have a script with: while getopts "h:loav" opt; do case $opt in h) h=$OPTARG echo $h ;; But whet I run it: $ ./ftpclean.sh -h ...
4
votes
1answer
126 views

How can I colorize head, tail and less, same as I've done with cat?

I've got 'color cat' working nicely, thanks to others (see How can i colorize cat output including unknown filetypes in b&w?). In my .bashrc: cdc() { for fn in "$@"; do source-highlight ...
1
vote
2answers
108 views

How to view source of “string.h” and other library function source code in gedit of Ubuntu?

I remember viewing source code of library function but now I don't know how to view it. Can someone help me viewing source code of library function?
0
votes
1answer
113 views

Why does a working standalone nested function/script not work inside a larger script? [duplicate]

The following (nested) function/s function hpf_matrix { # Positional Parameters Matrix_Dimension="${1}" Center_Cell_Value="${2}" # Define the cell value(s) function hpf_cell_value { ...
0
votes
1answer
71 views

Using break command as argument to function [closed]

What about use so solution? Functions run in loop (cycle?). In that loop - I have another function wuch also uses loop. When second function get NO answer from user - it send break 2 to stop loop and ...
3
votes
3answers
76 views

Is there something like closures for zsh?

I just decided to try zsh (through oh-my-zsh), and am now playing with precmd to emulate a two-line prompt that has right prompts in more than just the last line. So I clone the default theme, and ...
2
votes
2answers
125 views

bash: get array name from parameter to function with saving indexes

I have a function to show index of chosen element. I'm trying to pass a parameter to function to use it as an array name. This works: getIndex() { arrname=$1[@] b=("${!arrname}") index=1; while ...
0
votes
2answers
79 views

Why must you be careful when using Bash's built in command history function to re-run previous commands that contain variables?

I know !! re-runs commands but what exactly would occur if I re-ran a command that had a variable in the command?
3
votes
2answers
76 views

How to show last command with expanding function in bash

I'm using function like this. $ find-grep () { find . -type f -name "$1" -print0 | xargs -0 grep "$2" ; } After I type: $ find-grep *.c foo I want to get expanded last command string. In this ...
4
votes
2answers
308 views

How to test if command is alias, function or binary?

I have command foo, how can I know if it's binary, a function or alias?
1
vote
1answer
132 views

Execute command supplied by function parameters

I'm trying to create a function method in a bash script that executes a command which is supplied to the method by the paramters. Meaning somethings like this: special_execute() { # Some code ...
2
votes
4answers
245 views

Scope of variables when calling function from find

In a bash script I define a function that is called from find. The problem is that the scope of variables does not extend to the function. How do I access variables from the function? Here is an ...
0
votes
1answer
85 views

Converting a loop of code to function

I want to retry a command for 5 times with an interval of 20 seconds. I want this command to be passed as a method parameter. How to do it? And once the function is written how to pass the value to ...
1
vote
2answers
374 views

Bash function to scp a file not working

I am new to bash scripting and read basic tutorials online and wrote following simple bash function: function to_company() { scp ${1} username@long.server.name.company.com://home/username } ...
2
votes
3answers
445 views

Bash source — select the right function when two sourced files have the same function name?

My bash script sources a script file (call it file2.sh) according to an argument. (It is either sourced or not.) The script file2.sh contains a function "foo" (call it a modified or improved version ...
5
votes
3answers
217 views

Can you pipe to a .bash_profile function?

I received a great function for highlighting files in Apple's finder using the command-line. It's basically a wrapper for osascript. I got it from Mac OS X: How to change the color label of files ...
1
vote
2answers
2k views

How to return the exit code? Error: return: Reading: numeric argument required

Here's a simplified version of my script. My question is, How do I return the exit code from apt-get in this case? #!/bin/bash install_auto() { apt-get -h > /dev/null 2>&1 if [ $? -eq 0 ] ; ...
4
votes
2answers
187 views

Passing a code block as an anon. function

Is it possible to treat a block of commands as an anonymous function? function wrap_this { run_something # Decide to run block or maybe not. run_something else } wrap_this { do_something ...
3
votes
1answer
149 views

bash: Accessing function call stack in trap function

Working on a bash function call stack trace... Script traps errors and runs a callStack() function. But on trapping, It always shows a call stack for the callStack() function itself instead of the ...
2
votes
2answers
109 views

Pass arguments to function exactly as-is

I have the following function: bar() { echo $1:$2; } I am calling this function from another function, foo. foo itself is called as follows: foo "This is" a test I want to get the following ...
1
vote
1answer
235 views

how to locally redefine 'command_not_found_handle'?

I'd like to make a particular bash script failfast when it cannot find a command, while retaining globally the usual friendly command_not_found behavior. E.g., if I save the following to /tmp/foo.sh, ...
19
votes
4answers
917 views

difference between function foo() {} and foo() {}

I can define bash functions using or omitting the function keyword. Is there any difference? #!/bin/bash function foo() { echo "foo" } bar() { echo "bar" } foo bar Both calls to functions ...
5
votes
2answers
238 views

Gawk: Passing arrays to functions

Stuck with GNU awk 3.1.6 and think I've worked around its array bugs but still have what looks like a scope problem in a 600-line awk program. Need to verify understanding of array scope in awk to ...
3
votes
1answer
332 views

for loop in bash function

I recently wrote the following bash function: makeaudiobook () { count=1 almbumartist=$2 for f in $1; do preprocess $f > $f-preprocessed text2wave $f-preprocessed -o $f.wav ...
1
vote
2answers
131 views

Best way to call command within a shell function having the same name [duplicate]

I like to encapsulate commands within shell-functions using the same name. But to avoid the shell-function calling itself recursively, I specify the complete path of the command as the following ...
5
votes
2answers
363 views

How can I create a function in zsh that calls an existing command with the same name?

How can I write a function in zsh that invokes an existing command with the same name as the function itself? For example, I've tried this to illustrate my question: function ls { ls -l $1 $2 $3 ...
2
votes
1answer
207 views

How to get functions propagated to subshell?

Solaris / sh I have a few functions defined in a file which gets loaded via . ./some_file.sh When I start a subshell with sh All my function definitions are lost but when I do env I do ...
5
votes
2answers
149 views

How to define a Bash function that can be used by different scripts

I have defined a bash function in my ~/.bashrc file. This allows me to use it in shell terminals. However, it does not seem to exist when I call it from within a script. How can I define a bash ...
2
votes
3answers
364 views

What are commands to find shell keywords, built in functions and user defined functions?

I was discussing with my friend on how the commands are parsed in the shell, and he told me that bash searches the command in following order List of aliases List of shell keywords List of ...
8
votes
3answers
326 views

Running an executable in PATH with the same name as an existing function

Sometimes I define a function that shadows an executable and tweaks its arguments or output. So the function has the same name as the executable, and I need a way how to run the executable from the ...
4
votes
2answers
608 views

Bash function not working in Zsh

I have been slowly migrating from Bash to Zsh and have got to the point where everything I have moved across is working well, with one exception. I have a couple of functions in my .bashrc that I use ...
6
votes
3answers
175 views

Display the function body in Bash

I have setup several functions in my .bashrc file. I would like to just display the actual code of the function and not execute it, to quickly refer to something. Is there any way, we could see the ...
5
votes
1answer
457 views

what is the zsh equivalent of bash's export -f

So I started using zsh. I like it all right. It seems very cool and slick, and the fact that the current working directory and actual command line are on different lines is nice, but at the same time, ...
1
vote
1answer
144 views

How to reference a script-local dictionary in a Vim mapping?

Somehow I'm not able to execute the following mapping: function! s:MySurroundingFunctionIWantToKeep() let s:Foobar={'foo': 'bar'} map \42 :echo <sid>Foobar.foo<cr> endfunction call ...
2
votes
3answers
352 views

How to reverse-match a string in the Vim programming language?

I want to find the last index of any character in the [abc] set in the abcabc string but the search should start from the end of the string: " Returns the 0th index but I want the 5th. let ...
2
votes
2answers
292 views

Bash: passing braces as arguments to bash function

I love using the following pattern for searching in files: grep --color=auto -iRnHr --include={*.js,*.html,} --exclude-dir={release,dev,} "span" . I'd like, however, to have this one wrapped into a ...
4
votes
1answer
176 views

View shell function's current definition

So I am editing bashrc constantly, and I have a terminal open with a working function definition, although bashrc has been updated with a wrong function definition. (Because the definition do not ...
5
votes
3answers
730 views

How to set an alias on a per-directory basis?

Suppose you have an alias go, but want it to do different things in different directories? In one directory it should run cmd1, but in another directory it should run cmd2 By the way, I have an ...
9
votes
4answers
2k views

Executing user defined function in a find -exec call

I'm on Solaris 10 and I have tested the following with ksh (88), bash (3.00) and zsh (4.2.1). The following code doesn't yield any result: function foo { echo "Hello World" } find somedir -exec ...
4
votes
2answers
224 views

Function caller positional parameters

I need to read and write the positional parameters $@ of a function's caller. The Bash man page says that: A shell function is an object that is called like a simple command and executes a ...
2
votes
2answers
119 views

Is there any way I can fit this into my ~/.bashrc as a function?

I just discovered this useful bit of code on this useful-looking website. #!/bin/sh exec tclsh "$0" ${1+"$@"} proc main {} { set lines [lrange [split [read stdin] \n] 0 end-1] set count ...