# csh command history



## Spity (Jul 10, 2021)

Hi! I am using csh and I notice that I cannot see the command history. That is, I open a terminal, write a command, close it, and when I reopen it, it is clean. No history. How can I have the history there to speed up the writing?


----------



## SirDice (Jul 10, 2021)

Does ~/.history exist? That's the file where tcsh(1) writes its history to.

Note, it's csh(1), that's a shell. chsh(1) is a command to change account settings.


----------



## Deleted member 30996 (Jul 10, 2021)

Pushing the up arrow brings back the last commands I used as root. That's true for what I type in the terminal emulator if I exit the WM to the login terminal and work from there.


----------



## grahamperrin@ (Jul 10, 2021)

Spity said:


> … No history. …



What response to the `history` command?

I do sometimes get a sense of some things (not everything) missing from history, but I can never put my finger on it.


----------



## Spity (Jul 10, 2021)

SirDice said:


> Does ~/.history exist? That's the file where tcsh(1) writes its history to.
> 
> Note, it's csh(1), that's a shell. chsh(1) is a command to change account settings.


.history Does not exist. When I close my terminal and reopen it, it is empty without history of everything I typed before


----------



## Vull (Jul 10, 2021)

Spity said:


> .history Does not exist. When I close my terminal and reopen it, it is empty without history of everything I typed before


Please try `touch $HOME/.history` -- I tried this on my own system and it worked.

My non-root user account was originally created with /bin/sh as the shell. The sh shell does not use a .history file, and when I ran `chsh -s /bin/csh` it did not create such a file, but when I created the file using the `touch` command, it started working immediately.

You might need to log out and log in again; you'll want to do that anyway, to see if it starts remembering your history from the previous login session when you press the up-arrow key.


----------



## grahamperrin@ (Jul 11, 2021)

grahamperrin said:


> I do sometimes get a sense of some things (not everything) missing from history, but I can never put my finger on it.



…



Vull said:


> … log out and log in again; you'll want to do that anyway, to see if it starts remembering your history from the previous login session …



Ah! So, is ~/.history written only at exit time or log out time?

I just realised, the last update to the file was more than six hours ago: 


```
% ls -hl .history
-rw-------  1 grahamperrin  grahamperrin    29K 10 Jul 20:48 .history
% date
Sun 11 Jul 2021 02:40:54 BST
%
```


----------



## Vull (Jul 11, 2021)

grahamperrin said:


> …
> 
> 
> 
> ...


I think so but didn't actually check. I did notice that it remembered the commands I had entered during that login session before I created the file.

As the non-root user, first I entered the `chsh` command, and, as expected, nothing had changed: I still had the $ prompt, `echo $SHELL` still output /bin/sh, and I was still using the /bin/sh commands (for example, `x=x` did not produce a syntax error). So I then logged out and back in. Lo and behold, I had the /bin/csh shell and the % prompt, but no .history file, so I created it, logged out and back in again -- call this login session number 3. The history file had all the commands from login session number 2, but contained no commands from login session number 1.

This was all more or less as expected. Using the sh shell, it always remembers all the commands from the present login session, but none from previous login sessions. What I infer from that is that the sh shell keeps all the present session's commands in memory, but then loses that memory when you log out, whereas the csh shell writes them in the ~/.history file.

The /bin/bash shells I use in Linuxland also keep history, but in ~/.bash_history files. I don't use any other shells. I used to use ksh on AIX systems, but that was many years ago, and I could be mistaken, but I don't remember any of those old unixes keeping any history at all, even for the current login sessions. SCO used /bin/sh IIRR. I don't remember what shell the DEC systems used. 

Looking at my /bin/bash and /bin/csh history files right now, the last commands in those files is consistently `exit` in four of those files. Just guessing-- maybe you are shutting down your system without logging out some times?


----------



## grahamperrin@ (Jul 11, 2021)

It's probably true that in most (if not all) cases where I have been puzzled by sparseness of history, I had run (typically in ttyv1):

`shutdown -r now`

For the future, I'll try to remember to `exit` then log in before shuttting down …


----------



## Vull (Jul 11, 2021)

grahamperrin said:


> It's probably true that in most (if not all) cases where I have been puzzled by sparseness of history, I had run (typically in ttyv1):
> 
> `shutdown -r now`
> 
> For the future, I'll try to remember to `exit` then log in before shuttting down …


In FreeBSD, [CTRL][ALT][DEL] from a login prompt gives what appears to be a proper shutdown and reboot. I'm not sure if that works in Linux or not, because I never run Linux headless, so I just use the DE's [Restart] button instead.


----------



## grahamperrin@ (Jul 11, 2021)

I found some commits. In chronological order:

<https://cgit.freebsd.org/src/commit...c?id=ad4240fec4feed2dfca1ca0e0bb303eb01aa3a5b> _move all functions related to shutting down to one file called kern_shutdown.c …_
<https://cgit.freebsd.org/src/commit...c?id=3e755f76d1f51651901d3893554a92b8aa371684> _Make it possible to pass boot()'s flags to shutdown_nice() …_
<https://cgit.freebsd.org/src/commit/?id=acf0ab0669861bdcef9c49f5ed87ff6c82bd1d1b> _init: Only run /etc/rc.shutdown if /etc/rc was run. …_
<https://cgit.freebsd.org/src/commit/?id=912d59378b93b34f395d4e7e98078d76ec50005f> _Clean up shutdown_nice(). Just send the right signal to init(8). Right now, init(8) cannot distinguish between an ACPI power button press or a Ctrl+Alt+Del sequence on the keyboard. …_
*kern_shutdown.c « kern « sys - src - FreeBSD source tree* <https://cgit.freebsd.org/src/tree/sys/kern/kern_shutdown.c>


----------



## mer (Jul 11, 2021)

Interesting discussion.  It's often considered a bit of a security hole to have persistent shell history, so I've always had an "unset savehist" in my init file.

Also, if you are in a desktop environment, you probably need to close all term sessions or logout to force the save to file.


----------



## SirDice (Jul 11, 2021)

Spity said:


> When I close my terminal and reopen it, it is empty without history of everything I typed before


History is saved when you exit(1) the shell, not when you close it. So type `exit` (or use CTRL-D).

With csh(1) you can also force a history save with `history -S`.


----------



## grahamperrin@ (Jul 11, 2021)

Thanks,



SirDice said:


> With csh(1) you can also force a history save with `history -S`.



Below, what are the mysterious _first word_ and _second word_?


```
savehist
    If set, the shell does `history -S' before exiting.  If the
    first word is set to a number, at most that many lines are
    saved.  (The number should be less than or equal to the number
    history entries; if it is set to greater than the number of
    history settings, only history entries will be saved) If the
    second word is set to `merge', the history list is merged with
    the existing history file instead of replacing it (if there is
    one) and sorted by time stamp and the most recent events are
    retained.  If the second word of savehist is `merge' and the
    third word is set to `lock', the history file update will be
    serialized with other shell sessions that would possibly like
    to merge history at exactly the same time. (+)
```


----------



## Vull (Jul 11, 2021)

mer said:


> Interesting discussion.  It's often considered a bit of a security hole to have persistent shell history, so I've always had an "unset savehist" in my init file.
> 
> Also, if you are in a desktop environment, you probably need to close all term sessions or logout to force the save to file.


Kind of makes a case for using /bin/sh for security's sake. Which reminds me, after `touch ~/.history` one should probably execute `chmod 600 ~/.history` to tighten security on one's history file.

In my test case I just deleted the ~/.history file and ran `chsh -s /bin/sh` via my test system's non-root user, to revert back to using the relatively more secure sh shell.


----------



## SirDice (Jul 11, 2021)

grahamperrin said:


> Below, what are the mysterious _first word_ and _second word_?




```
set savehist = (2000 merge)
```


----------



## rorgoroth (Jul 11, 2021)

I love tcsh but the history is a nightmare. I've tried a whole bunch of stuff to get it to save right but it never does.

open term, type 1 [enter] 2 [enter]
open a second term, type 3 [enter] 4 [enter]
back to first term, type 5 [enter] 6 [enter]
close term2, close term 1.

Open new term, history:
1
2
5
6

The second terms history disappears rather than merging in. It seems like the histfile is locked by the first instance to start - no idea if this is how it works because I'm too lazy to read manpages and googling offers very little help for tcsh these days.

No amount of binds with history -s, prealias, etc make it work how I like.


----------



## SirDice (Jul 11, 2021)

rorgoroth said:


> close term2, close term 1.


Type `exit`.


----------



## rorgoroth (Jul 11, 2021)

SirDice said:


> Type `exit`.


my bad, that's what I do. I have ctrl+d set to exit


----------



## grahamperrin@ (Jul 11, 2021)

rorgoroth said:


> … tcsh …





rorgoroth said:


> … I have ctrl+d set to exit



I have not knowingly set it, but it seems to be effective.

With tcsh already my preferred shell for the root user, things seem OK for me, since learning from SirDice

With multiple tabs in Konsole, at a root command prompt I keyed *Up* and observed:

`root@mowa219-gjp4-8570p:~ # pkg provides xkeyboard-config`

After keying *Control-D* I brought forward a non-root tab, `su -`, entered the passphrase then keyed *Up* and observed the same ☑


----------



## SirDice (Jul 11, 2021)

grahamperrin said:


> I have not knowingly set it, but it seems to be effective.


It's the default keybinding.

```
"^D"           ->  delete-char-or-list-or-eof
```
It has multiple meanings, it also acts as a delete and it shows a list (when using completion, bash uses double TAB for that) too.


----------



## Erichans (Jul 13, 2021)

rorgoroth said:


> I love tcsh but the history is a nightmare. I've tried a whole bunch of stuff to get it to save right but it never does.
> 
> open term, type 1 [enter] 2 [enter]
> open a second term, type 3 [enter] 4 [enter]
> ...





SirDice said:


> ```
> set savehist = (2000 merge)
> ```



It has been reported that tcsh(1) (as of version 6.21) does not handle the merge option for the savehist shell variable correctly: Astron-tcsh-mailing-list . The error has been corrected in tcsh version 6.22. Starting at 2021-03-14 tcsh 6.22 has been imported into -CURRENT: source-log.

Any reasonable expectations or educated guess if tcsh 6.22 will find its way into 13.X-RELEASE? 
I'd guess unlikely but I do not have any knowledge or experience how such things tend to develop.


----------



## SirDice (Jul 13, 2021)

Erichans said:


> Any reasonable expectations or educated guess if tcsh 6.22 will find its way into 13.X-RELEASE?


It's been merged into 13-STABLE some time ago, so it will be included in 13.1-RELEASE.

```
root@molly:~ # tcsh --version
tcsh 6.22.04 (Astron) 2021-04-26 (x86_64-amd-FreeBSD) options wide,nls,dl,al,kan,sm,rh,color,filec
root@molly:~ # uname -a
FreeBSD molly.dicelan.home 13.0-STABLE FreeBSD 13.0-STABLE #33 stable/13-n246220-a7761d19dac: Sat Jul 10 14:23:54 CEST 2021     root@molly.dicelan.home:/usr/obj/usr/src/amd64.amd64/sys/MOLLY  amd64
```

It's also merged in 12-STABLE, so 12.3-RELEASE will have it too. 





						src - FreeBSD source tree
					






					cgit.freebsd.org


----------



## grahamperrin@ (Jul 13, 2021)

Reading <https://cgit.freebsd.org/src/log/contrib/tcsh?h=stable/11> alongside <https://cgit.freebsd.org/src/log/contrib/tcsh?h=stable/12> 

Is this an example of stable/11 simply receiving less attention? 

(Not complaining. I'm just curious.)


----------



## SirDice (Jul 13, 2021)

grahamperrin said:


> Is this an example of stable/11 simply receiving less attention?


The whole 11 branch will be EoL at the end of September.


----------



## Deleted member 30996 (Jul 16, 2021)

On my World,  I have a ~/.cshrc and a ~/.shrc config file but no ~/.history. 






I use the up arrow to re-issue commands I've already used during that session from the usr or root account going back to when I first logged on. And there it is on tcsh 6.21.00 to bring up a command just ran in that terminal.


----------



## jmos (Jul 16, 2021)

The screenshot says the shell is "sh", not "(t)csh"; The corresponding manpage doesn't mention a history file, so IMO it's logic that there is none.


----------



## Deleted member 30996 (Jul 16, 2021)

By default. 
If there isn't one and everything is working as it should, how could I miss it? Much less know I was missing something I never needed in the first place?

I'm going to have to do a search on my posts and document how many things I do differently or not at all that seem to be SOP for everyone else. There are several files I'd never touched before somebody else mentioned using them. fstab only one among them.

That comes from teaching myself how to use it as a PC-BSD use rand many of the things I do carried over from then. I did not think the Handbook applied and never read it. Teaching myself to use ports what was hardest. a post asking where to find portsnap in the ports tree where I was at in 2005. 

Determination and a desire to use UNIX, the association with Ma Bell what impressed me, Trial and Error, google-fu, and natural ability and aptitude by birthright what got me, Asking questions embarrassing for me, kept me quiet and hidden among you while I learned how to do things that came hardest of all, like setting my email alias. 

That finally figured out well after I joined, I'm talking maybe 2 years, and still did not read the Handbook. Now it all comes together for me easily when nothing is easy anymore.


----------



## Deleted member 30996 (Jul 16, 2021)

The `tcsh --version` command taken from the post of SirDice, ran from my user account and using the up arrow to scroll back through commands I had entered into that terminal as shown by screenfetch.


----------



## SirDice (Jul 16, 2021)

FreeBSD's sh(1) has some rudimentary command line history but as far as I know that history is not saved. It's for that session only and is stored in memory. If you exit that shell the history is lost. The history of tcsh(1) is saved to ~/.history and persists after closing the shell and reopening it. Bash uses ~/.bash_history to store its persistent history.


----------



## Profighost (Feb 19, 2022)

Under FreeBSD13 I have no saved history as root.
Of course my root's shell is csh, and also when I change it to tsch no .history exists.

Besides the fact that is very annoying, especially if one has to do some work as root such as setting up a new system on a new machine...
Was there anything changed with the new FreeBSD or may I activated a "system hardening option" while installation which prevents root's history?


----------



## grahamperrin@ (Feb 19, 2022)

Profighost said:


> … my root's shell is csh,



`sudo grep set\ history /root/.cshrc`

What's yours?

Here, with FreeBSD 14.0-CURRENT:


```
% grep set\ history ~/.cshrc
        set history = 2000
% sudo grep set\ history /root/.cshrc
grahamperrin's password:
        set history = 2000
%
```



Profighost said:


> and also when I change it to tsch no .history exists. …




```
root@mowa219-gjp4-8570p-freebsd:~ # echo $SHELL
/bin/csh
root@mowa219-gjp4-8570p-freebsd:~ # /bin/tcsh
root@mowa219-gjp4-8570p-freebsd:~ # echo $SHELL
/bin/csh
root@mowa219-gjp4-8570p-freebsd:~ # history | tail -n 2
  2001  8:45    echo $SHELL
  2002  8:46    history | tail -n 2
root@mowa219-gjp4-8570p-freebsd:~ # exit
exit
root@mowa219-gjp4-8570p-freebsd:~ # exit
logout
%
```


----------



## Profighost (Feb 19, 2022)

The .cshrs is the same as on my old 12.3-RELEASE machine:

set history = 1000
        set savehist = (1000 merge)
        set autolist = ambiguous

and yes, echo $SHELL shows me I'm using tcsh or csh - that's why I am puzzled

there simply is no .history-file within my root's ~


----------



## grahamperrin@ (Feb 19, 2022)

Profighost said:


> FreeBSD13



`freebsd-version -kru`


----------



## Profighost (Feb 19, 2022)

freebsd-version shows me, what I already know: 
13.0-RELEASE-p7
freebsd-version -kru shows me:
13.0-RELEASE-p7
13.0-RELEASE-p7
13.0-RELEASE-p7
...I don't see the point.
(I have to manually type all this, since I neither have x-server running yet on that machine, not even ssh configured yet.)

No root-history, no xserver running...
I'am now trying to install FreeBSD 12.3


----------



## covacat (Feb 19, 2022)

start csh
set |grep hist
see if you have a savehist line


----------



## Profighost (Feb 19, 2022)

yep:
savehist        (1000 merge)


----------



## Profighost (Feb 19, 2022)

Suspicion:
I am still within the console - no xserver, no xterm.
May .history only functions within "higher" terminals?


----------



## covacat (Feb 19, 2022)

try history -S 
that shoud save it just then
also see where $HOME and $home point


----------



## Profighost (Feb 19, 2022)

Both correctly pointing to /root

After #history -S I do have also a .history after reboot and also the history seems to work as usual.
*THANKS*

I'll continue to watch this thread. 
Maybe additonal ideas will be brought up with further explanations, what causes my issue.
I'm not satisfied with just copy solutions from the internet but try to understand things.
As far as I understand this issue: 
There simply was no .history (I actually didn't had one). Without a .history also non is written. 
Now since one is generated with #histroy -S there is one and now it works.

I can't help it that there something really stupid, silly and very simple is not in the right order... 
(Maybe within 13 it was simply forgotten to intialize a root's .history by default - I presume most of you guys updated to and moved to the higher version, but not actually restarted with 13 at completely zero, so this issues wasn't detected yet[?]) 
(I can tell you, if you are used to history and then there suddenly is none anymore, especially while getting up a new system, you'll start getting very p#$$** in no time!   )

However - it seems to work now. 
So I'll continue with my main target and get this xserver up and running.


Thanks again all for all your effort and help so far:
Thank you

(btw: your cat is sweet)


----------



## grahamperrin@ (Feb 19, 2022)

Profighost said:


> ...I don't see the point.



It was for us to see the information (thanks).


----------



## Erichans (Feb 19, 2022)

Profighost said:


> Under FreeBSD13 I have no saved history as root.





Profighost said:


> The .cshrs is the same as on my old 12.3-RELEASE machine:
> 
> set history = 1000
> set savehist = (1000 merge)
> set autolist = ambiguous


As you're moving between 12.3-RELEASE and 13.0-RELEASE the (t)csh version number will be different (in this particular case the tcsh-version number of 13.0 is lower because 13.0 came out first, _before_ 12.3), this may be of interest to you with respect to the correct functioning of the history mechanism, see here


----------



## dann (Nov 9, 2022)

When using X is good practice to set savehist with a 3rd argument 'lock' for the history file update to be serialized with other shell sessions.

```
set savehist = (2000 merge lock)
```


----------

