For example, I want to make a command good night and this would look something like alias good night="many many many commands here". I tried this but, whitespace is not allowed. Is there any way I could accomplish this?
Asked
Active
Viewed 8,557 times
3
wjandrea
- 14,109
- 4
- 48
- 98
ujwal dhakal
- 191
- 1
- 2
- 7
-
1Try to use good_night, or assign an alias to good, and an alias to night, and see what happens. I think it could work. – GTRONICK Jan 12 '17 at 16:06
-
i just want to know if its possible i just want white space sir – ujwal dhakal Jan 12 '17 at 16:07
-
2You could make a script or Bash function that is named `good`, which reads its arguments, requires the first one to be "night" and then executes the commands you want. – Byte Commander Jan 12 '17 at 16:11
-
Aliases are deprecated. Use a function instead. – wjandrea Jan 12 '17 at 17:44
-
@ujwaldhakal Why do you need the whitespace? Why not `goodnight`? – wjandrea Jan 12 '17 at 18:49
-
@wjandrea ours is not to question but to answer ;) – Rinzwind Jan 12 '17 at 19:17
-
@Rinzwind Lol, I know, but this feels like an XY problem. – wjandrea Jan 12 '17 at 21:52
-
@wjandrea see what question wants to explain.. making whitespace in command alias – ujwal dhakal Jan 13 '17 at 03:50
-
@ujwaldhakal I'm not sure what you mean – wjandrea Jan 13 '17 at 04:08
1 Answers
10
This function should get you started:
good () {
if [ -z "$1" ]; then
echo "Perhaps you meant 'good night'?"
else
if [ "$1" = "night" ]; then
echo "GOOD"
echo "NIGHT"
echo "good"
echo "night"
echo "etc"
else
echo "ERROR: strange time detected: $1"
fi
fi
}
Save it as, for example, good.sh, then source it:
. good.sh
good night now will execute various commands (replace the echo statements with whatever you want).
-
1You can add the line that sources your script to the end of your `~/.bashrc` file so that the function will be available in every Bash session. – Byte Commander Jan 12 '17 at 16:18
-
Or put the function itself in the bashrc, which may be more convenient. – wjandrea Jan 12 '17 at 18:43
-
1You could simplify the logic a bit by removing the `if [ -z "$1" ]` statement. It's not needed. – wjandrea Jan 12 '17 at 18:53