I want to use the !!, !$,^abc^cde, etc. aliases in a bash script.
Sadly, I cant seem to make them work in a script (They work just fine in interactive mode),
Does anybody know what the problem is? If this can not be done, do you know about anything that operates in a similar way that can be used in a bash script?
-
What you're asking for is "history expansion". It's turned off by default in scripts, and is considered bad practice to use it in them anyhow.Charles Duffy– Charles Duffy2013-05-12 16:38:07 +00:00Commented May 12, 2013 at 16:38
Add a comment
|
1 Answer
Use set +o histexpand to enable history expansion in non-interactive bash shells.
5 Comments
Charles Duffy
To be clear -- this is non-POSIX; it may work in bash scripts, but isn't guaranteed to work in any other shell.
Teddy
@CharlesDuffy Well of course it is bash-specific. The question has the "bash" tag, and the question mentions bash in the question and in the title.
Charles Duffy
Sure. I didn't say it was wrong. Keeping track of what is and isn't portable is important.
Teddy
@CharlesDuffy A Bash question gets a Bash answer and does not need a POSIX non-portability disclaimer, just like a Linux programming answer does not need a Windows non-portability disclaimer.
Charles Duffy
StackOverflow answers are for a general audience, not just the person originally asking the question. Answering the initial question is relevant to scoring and acceptance, sure, but it's worth making answers comprehensive.