Reputation: 1083
I wanna make some custom commands for my terminal (i'm using Ubuntu).
I've already learned that i need to, for example, edit '.bash_aliases' file (in /home/your_user_name/), type 'source ~/.bash_aliases', and it should work then.
Well some things really works, like if i write (in '.bash_aliases') something like:
my_comm(){
if [ "$1" = aaa ]; then
echo hi a
fi
if [ "$1" = bbb ]; then
echo hello b
fi
#echo this is a comment :]
echo ending echo
}
then if i'll save file, type 'source ~/.bash_aliases', and run:
my_comm
it will print:
ending echo
and writing
my_comm bbb
will give:
hello b
ending echo
That's nice, but i want to know few more things, and i can't find them by google :(
(1) how can i set a variable and then get the variable?
like:
var myVar = "some_dir"
cd /home/user/'myVar'/some_sub_dir/
?
(2) i wanna make a function to shortcut a find/grep command that i use often:
find . -name "var_1" -print0 | xargs -0 grep -l "var_2"
I did something like:
ff(){
find . -name '"$1"' -print0 | xargs -0 grep "$3" '"$2"'
}
so, now executing:
ff views.py url -l
should give me:
find . -name 'views.py' -print0 | xargs -0 grep -l 'url'
but instead i recive:
grep: find . -name "$1" -print0
: There is no such file or directory
help pls :)
Upvotes: 0
Views: 844
Reputation: 2426
You can even using alias
keyword for single instructions or use function
keyword and combine couple of instructions in one. you can have a look at this
Upvotes: 0
Reputation: 21507
(1) how can i set a variable and then get the variable?
Like this:
myVar="/long/name/may have/a space/"
....
cd /home/user/"$myVar"/someSubDir.
Double quotes don't prevent variable substitution (unlike single quotes).
(2) i wanna make a function to shortcut a find/grep command that i use often:
find . -name '"$1"' -print0 | xargs -0 grep "$3" '"$2"'
You achieve nothing useful with multiple kind of quotes here; actually you prevent $1
and $2
from being substituted and that breaks your function. Try this:
find . -name "$1" -print0 | xargs -0 grep "$3" "$2"
Upvotes: 2