Solved Where to save my own shell scripts

carlcaulkett

Member

Reaction score: 3
Messages: 85

Hello, I am hoping to write a couple of shell scripts (such ambition!) and I want to make sure that I start off with using shell scripts in a correct and idiomatic manner. My first observation has been that I should aim for basic sh compatibility and use the #! /bin/sh shebang. What isn't so obvious though, even after an hour of googling (honest!) is where my shell scripts should be saved. Should I create a new folder and add it to the path, or is there an existing folder within the default path which is suitable to use. My attempts so far, to use /usr/local/bin, for instance, have met with permissions problems, presumably because such folders are not meant for user script files. Please, could someone advise me? Thanks.
 

rufwoof

Active Member

Reaction score: 76
Messages: 235

If a script is just for me (user), I create a ~/bin folder and add that to my PATH. If for other users to share then /usr/local/bin

man hier shows some other/useful detail.
 

cellini

Active Member

Reaction score: 2
Messages: 100

You also have to chmod +x filename and I normally rehash the shell so the tab complete works before running the command.
 

scottro

Daemon

Reaction score: 569
Messages: 1,560

For what it's worth, I believe that $HOME/bin is automatically in a users $PATH.
 
OP
carlcaulkett

carlcaulkett

Member

Reaction score: 3
Messages: 85

If a script is just for me (user), I create a ~/bin folder and add that to my PATH. If for other users to share then /usr/local/bin

man hier shows some other/useful detail.
That sounds good enough to me. What's the best startup file to assign the path to the new folder. I was warned off using /etc/rc.conf since this is meant to be used solely for assigning OS variables, apparently.
 

shepper

Aspiring Daemon

Reaction score: 305
Messages: 850

Part of this depends on the intended users for your scripts. If it is just you, the folder can be in your home directory. If they are system wide, I think /usr/local/libexec is preferred (this is where wblock@ put the filter scripts in the printing section of the handbook).
 

phoenix

Administrator
Staff member
Administrator
Moderator

Reaction score: 1,278
Messages: 4,099

That sounds good enough to me. What's the best startup file to assign the path to the new folder. I was warned off using /etc/rc.conf since this is meant to be used solely for assigning OS variables, apparently.
That would depend on your shell. You set (and export into the environment) the PATH variable in the config file for your shell.

For bash, it's .bashrc. For zsh, it's .zshrc. For csh, it's .cshrc. And so on.
 
OP
carlcaulkett

carlcaulkett

Member

Reaction score: 3
Messages: 85

My investigation of my system has revealed something confusing. The PATH used by my root login appears to be set in /root/.cshrc which is a csh type shell script, so the PATH syntax there of the form set path = (/sbin /bin /usr/sbin /usr/bin /usr/local/sbin).

The script which governs my user login is $HOME/.shrc which is a sh type shell script, and seems to have a completely different syntax for setting the PATH.

What do I need to do to get my user login to use the same syntax to set the PATH as root? Or have I totally misunderstood what is going on here?

I've been used to hacking in macOS for the last 9 months (and Windows for the previous 25 years or so!). Despite the links with macOS, it's clear that FreeBSD is set up in a much stricter fashion than macOS. It's like programming in Golang in that it is quite opinionated and basically says this is the way we do it around here. Our way or the highway! I like that!
 

ShelLuser

Son of Beastie

Reaction score: 1,812
Messages: 3,600

What do I need to do to get my user login to use the same syntax to set the PATH as root? Or have I totally misunderstood what is going on here?
You really don't want to. The root user is best treated differently than the rest because of what account it is and the kind of access it has. For example: most regular users can't do much with the tools in /sbin so it doesn't necessarily make sense to include that in their search path, but that doesn't apply for root.

Also, as phoenix also mentioned: a lot of this depends on the shell being used.

But if you want a more general setup then look into /etc/profile. It's the global profile for /bin/sh which is pretty much the default shell for regular use.

Also note that with FreeBSD it really isn't "one way or the highway". Nothing is stopping you from completely changing things to your liking. For example... it is strongly advised to leave the shell for root to /bin/csh because of it's interactive nature. Personally I also think the difference between a bourne (-related) shell and the C shell are beneficial because if you type up a hasty command while being root then chances are that it won't work. I consider that a good thing because root shouldn't be used casually. But that doesn't have to stop you from changing the root shell from /bin/csh into, say, /usr/local/bin/bash. Not advisable, but still easily doable.
 

scottro

Daemon

Reaction score: 569
Messages: 1,560

I have an old article, probably more important when it was more common to put everything on separate physical partitions about csh and root. I really would not change root's shell.
http://daemonforums.org/showthread.php?p=4598

Back to path's. As a user, if you run echo $PATH you should see that $HOME/bin is part of the path. If you want to add paths, as has been said, the $HOME/.$SHELLrc file is the usual place. In FreeBSD,with bash, however, I've found that I have to create a $HOME/.bash_profile file that just reads

source ~/.bashrc

or bash won't read my $HOME/.bashrc file.

These days I usually use bash, so I'm not sure about other shells. Years ago, at least, that used to be the same for ksh, though rather than creating a ksh_profile, it would use the existing profile file.
 
OP
carlcaulkett

carlcaulkett

Member

Reaction score: 3
Messages: 85

Also note that with FreeBSD it really isn't "one way or the highway". Nothing is stopping you from completely changing things to your liking.
Point taken. If FreeBSD itself isn't opinionated, then the users certainly are! Again, I think this is a good thing. I'd rather have a computing environment where there is a clear sense of the right way to do things.
 

jrm@

Daemon
Developer

Reaction score: 476
Messages: 1,213

I create ~/local and supply that as the prefix for applications that I install just for my account. After installing a few applications there, the hierarchy under ~/local looks similar to the one under /usr/local.
Code:
% ls ~/local
bin lib man var
Scripts that I create then go in ~/local/bin, which is in $PATH.
 
Top