-1

I try to write a simple script and store all my favor commands such as grep, find, sed.

when I use history command in shell, I can show all the historical commands with (history) I can reuse the the symbol '!'(Exclamation) to get my old commands:

Let's assume 2099 -> ls -lah (in my history)

>!2099 <- ENTER

>ls -la[CURSOR]

when I type '!2099 <- ENTER', 'ls -la[CURSOR]' in the next line.

does anyone know how to implement it on shell script?

I have tried to remove the '\r' from the input string, but it does't work so far.

closed as unclear what you're asking by Michael Homer, Stephen Rauch, Romeo Ninov, Stephen Kitt, G-Man Dec 7 '17 at 0:38

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

0

The fc command allows you to edit and list commands from your history. This is useful when scripting anything related to history. List entries with:

fc -ln [first] [last]

-l is the list flag, -n prevents display of the history numbers, [first] and [last] are the numbers from the history command. To list a single command repeat its number. So using your example:

cmd=$(fc -ln 2099 2099)
echo "$cmd"

will display ls -la. The output has a tab at the beginning so you can strip that off by piping fc through sed 's/^\t//' or tr -d '\t'

Not the answer you're looking for? Browse other questions tagged or ask your own question.