Tagged Questions
0
votes
0answers
48 views
Using an alias or function to run process in background, with dot files [closed]
Drews-MacBook-Pro:5493040 drewgross$ function s() { subl "$@" & }
Drews-MacBook-Pro:5493040 drewgross$ s .profile
[1] 1268
-bash: .profile: command not found
Drews-MacBook-Pro:5493040 drewgross$
...
1
vote
1answer
41 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, ...
12
votes
4answers
461 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 ...
3
votes
1answer
209 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
...
4
votes
2answers
95 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 ...
8
votes
3answers
163 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 ...
3
votes
2answers
106 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 ...
2
votes
2answers
181 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 ...
3
votes
2answers
156 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 ...
3
votes
2answers
167 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
93 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 ...
1
vote
1answer
155 views
Switching source and destination (or undoing the mv, cp operation)
mv or cp commands both expect source and destination as arguments.
In case you want to undo the change you made, or just change the source and destination you supplied before, what is the quickest ...
-1
votes
1answer
136 views
Interactive commands in Bash functions [closed]
How can I make a function interactive when using commands like ssh and less inside of a function?
function spo{
ls | less
}
For example how can I interact with less?
1
vote
2answers
506 views
Script to create files in a template
I just wrote a function in my ~/.bashrc that will let me create a folder for a new website with one command. The function looks like this:
function newsite() {
mkcd "$*" # mkdir and cd into it
...
87
votes
9answers
5k views
In Bash, when to alias, when to script, and when to write a function?
It's taken me almost 10 years of Linux usage to ask this question. It was all trial and error and random late-night internet surfing.
But people shouldn't need 10 years for this. If I were just ...
7
votes
2answers
360 views
How do I redefine a bash function in terms of old definition?
Is there any way I can redefine a bash function in terms of its old definition? For example I would like to add the following block of code to the preamble of the function command_not_found_handle (),
...
4
votes
3answers
2k views
Can I “export” functions in bash?
source some_file
some_file:
doit ()
{
echo doit $1
}
export TEST=true
If I source some_file the function "doit" and the variable TEST are available on the command line. But running this script:
...
3
votes
3answers
1k views
How to pass a string parameter on bash function?
I have this code that does work:
get_parameter ()
{
echo "$query" | sed -n 's/^.*name=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"
}
But I want to replace the "name" with the parameter that I pass ...
4
votes
2answers
198 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 ...
2
votes
3answers
233 views
Prefix every argument with -o in BASH
How do I prefix -p to every argument passed to my function?
Modifying the arguments themselves and creating a new array are both fine.
4
votes
4answers
483 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 ...
1
vote
3answers
1k views
alias or bash function does not work
When I create
alias wd='ps -ef | grep java | awk {'print $2 " " $9'} | egrep "(A|B|C|D)"'
or
function wd () {
ps -ef | grep java | awk '{print $2}' ...
}
in my .bashrc file, I get errors. ...
1
vote
4answers
1k views
how can i use sudo within a function?
i have written a function which acts in a similar way to tee but also pre-pends a datestamp. everything works fine except when i want to output to a file which is only root writeable (in my case a ...
4
votes
4answers
490 views
How to make functions created in a bash script persist like those in .bashrc?
My .bashrc was getting a little long, so I decided to break it up into smaller files according to topic and then call these files from within .bashrc as so
#my long .bashrc file
bash .topic1rc
bash ...
2
votes
2answers
2k views
Passing a variable to a bash script when sourcing it in another bash script?
Suppose I have in main.sh:
NAME="$HOME"
if [ -f "$HOME/install.sh" ]
then
. "$HOME/install.sh" "$NAME"
fi
and in install.sh:
echo $1
This is supposed to echo /home/user/, but it echoes ...
10
votes
1answer
905 views
bash 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 ...