Home > OS >  Loop history commands from first to 10 using Bash terminal
Loop history commands from first to 10 using Bash terminal

Time:10-28

I'm should use until loop to get first 10 commands from history line by line.

Tried something like:

counter=0

until [ $counter -gt 10 ]
do
  echo !$counter
  ((counter  ))
done

But output is docounter ten times.

The main issue is how to get inside loop specific line from history.

CodePudding user response:

Csh-style history expansion is an interactive feature; it does not work in scripts.

It's looking like you are simply looking for history $HISTSIZE | head -n 10

CodePudding user response:

There are a few simple ways. Try this -

 while read -r cmd; do if ((ctr   < 10)); then echo "$cmd"; fi; done < "$HISTFILE"

or

history|head -10|mapfile -t h && for c in {0..9}; do echo "${h[c]}"; done

edit

The terminal you are using at tutorialspoint kinda sucks.

Try it this way, and pay attention to why it matters.

history | while read -r cmd; do if ((ctr   < 10)); then echo "$cmd"; fi; done

Specifically, bash: /tmp/.bash_history: Permission denied

They are apparently only allowing access to the history file through the history program.

  • Related