2
votes
1answer
54 views

How can I get this script file's functions to load without having to source it every time? “command not found” (Bash/scripting basics)

How can I get this script file's functions to load without having to source it every time? I created a file foo with script functions I'd like to run. It's in /usr/bin, which is in the PATH. File ...
2
votes
2answers
127 views

How to force watch to run under bash

The problem is that when watch is executed it runs sh and I get this error: sh: 1: func1: not found here is the code: #!/bin/bash func1(){ echo $1 } export -f func1 watch func1
0
votes
1answer
66 views

apply bash function to parent scope

I can to define function in bash and use it: foo() { echo $1; } foo test But if I want to collect my functions in one bash script all its unavailable: init.bash #!/bin/bash foo() { echo $1; } ...
1
vote
2answers
26 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
2answers
66 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 ...
3
votes
3answers
3k 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 ...
3
votes
3answers
94 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 ...
5
votes
2answers
415 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?
0
votes
1answer
93 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 ...
5
votes
3answers
257 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 ...
20
votes
4answers
1k 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 ...
1
vote
2answers
198 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 ...
2
votes
1answer
417 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 ...
2
votes
4answers
550 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 ...
4
votes
2answers
946 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 ...
4
votes
2answers
287 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
1answer
275 views

awk function with a number parameter for the column you want to print

I want to use my awk shortcut as a function, so that I can pass the column number which then prints me the output. My aliases are: alias A="| awk '{print \$1}' alias G="| grep -i' Instad of typing: ...
2
votes
1answer
94 views

Restoring an option at the end of a function in zsh

I'm writing a zsh shell function (as opposed to a script) where I would really like the extended_glob option to be enabled. But since the function runs in the caller's context, I don't want to clobber ...
4
votes
2answers
282 views

Combine two commands in .bash_profile

In my .bash_profile file, I'd like to setup a single command alias that is two commands that execute one after another. The first command takes an argument from the command line and the second is ...
-1
votes
3answers
484 views

calling functions within a function

I need to fetch the results of different time ranges and email the results, but i am having trouble with the email part of the script, how can i send an email with both results? (15min range and ...
4
votes
4answers
1k views

Is it possible to source a file in bash, but skipping specific functions?

Suppose I have bash_functions.sh: function test(){ } function test2(){ } And in my ~/.bashrc I do: source ~/bash_functions.sh Is it possible to, when sourcing it, avoid sourcing a specific ...
18
votes
1answer
2k views

Aliases vs functions vs scripts

This site says, "Shell functions are faster [than aliases]. Aliases are looked up after functions and thus resolving is slower. While aliases are easier to understand, shell functions are preferred ...