Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Sometimes I do the history command so I can see 4 commands in a row.

I want to copy those commands so I can run them again.

I usually select them with the mouse and shift-ctrl-c which works ok, but I also get the line numbers.

How can I do history and not have line numbers?

I tried the man page but was overwhelmed.

share|improve this question
    
You are probably talking about the shell built-in history command which has no manpage but a help page, run help history to see it. –  scai Aug 13 '12 at 13:32
1  
Actually man history works find on my machine but there is about 20 mins of readable material and I was wondering if anyone knew the answer quickly, SO style. –  Michael Durrant Aug 13 '12 at 13:34
    
@Michael-Durant here the history manpage is about the The GNU History Library, I doubt yours is about the shell-builtin. –  scai Aug 13 '12 at 13:38
    
Ah, that's helpful. thk. –  Michael Durrant Aug 13 '12 at 13:52

4 Answers 4

up vote 1 down vote accepted

Using awk:

history | awk '{$1="";print}'

share|improve this answer
    
It needs to have a space between each to work right –  Michael Durrant Aug 13 '12 at 13:31
    
I added the space. Now I notice that, not too surprising, it only works for the first 3 items separated by a space, the rest get ignored. –  Michael Durrant Aug 13 '12 at 13:37
1  
This only returns 3 parts of each command line, I don't think that is what the OP asked for. –  Thor Aug 13 '12 at 13:40
1  
@Tim history | awk '{$1="";print}' will work much better ;) –  rush Aug 13 '12 at 13:52
    
I've actually updated this "awk" answer with that as it is a much better solution. Also I can then reverse my downvote. I don't like down-votes and use them sparingly. –  Michael Durrant Aug 13 '12 at 14:01

Note that history output may depend on local environment. On my machine it looks as:

 $ history | tail -1
 2993  2012-08-13 17:42:17 echo "test"

and none of your answers will work at my side.

A useful option is fc (usually it is builtin) which works perfectly independent from your local settings.

fc has -n option that suppresses numbers in output. So your command would look like:

fc -l -n

it outputs only the several last commands.

To output the whole history in this style use:

fc -l -n 1  

To output without spaces at the begin you can use sed to remove them:

fc -l -n 1 | sed 's/^\s*//'
share|improve this answer
    
Works although gives a lot of spaces at the start of the line. So far I prefer Thor's answer. Thanks. –  Michael Durrant Aug 13 '12 at 13:52
    
@MichaelDurrant you can delete all trailing spaces with sed 's/^\s*//'. I've just added an option with it to my answer. –  rush Aug 13 '12 at 13:55

In bash:

history | tr -s ' ' | cut -d' ' -f3-

In zsh and ksh:

history -n

In tcsh:

history -h

Edit

Also see rush's answer about using fc.

share|improve this answer
    
Yeah that's more like it –  Michael Durrant Aug 13 '12 at 13:40
    
Yup and with an alias nhistory='history | tr -s ' ' | cut -d' ' -f3-' in my ~/.bash_aliases file I now have nhistory for "numberless" history at my fingertips. I was tempted to use nnhistory but I also go for less characters when I can for quicky aliases. –  Michael Durrant Aug 13 '12 at 13:43

An alternative solution is to use the terminal's block select method, if it has it available. Usually you simply hold Ctrl before selecting with the mouse. This will enable block select mode in many modern terminals (definitely the numerous terminals based on libvte).

Next, simply drag and release so your selection block covers all the lines (up to the full width of the longest command) you want to copy. Then you simply paste it, and newlines will be preserved.

This is a simple trick that I use many times per day when doing quick copy/pastes, to avoid things like prefixed line numbers, timestamps in logfiles, etc.

Note that this will not work if one of the copied commands spans more than one line!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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