DEV Community

loading...

Source or Symlink or Other?

Dan
I’m a coding guy
・2 min read

Source or Symlink or Other?

I'm looking for some insight. I started getting into bash/terminal/etc on my mac since around January and now I use it quite a lot. I started making some small scripts to automate some things that I do and it's great!

I realized when I started that I wanted to keep scripts in a different place (cloud). I don't need to sync anything to other machines, but it's just convenient for the once in a while format that I do. So I created the following set up:

  • Everything is stored in my dropbox ~/dropbox/.bash/scripts/
  • I have a script called init.sh that will look in ~/dropbox/.bash/scripts/ and then source each script. So then each script is I believe loaded into memory(?). init.sh also contains many other things like my prompt customization etc.
  • Each script contains a function - and so when I use the script I'm not using the script name, just a function name (since I source it.
  • my ~/.bash_profile only has one line: source ~/dropbox/.bash/init.sh

So for example, a script called hello.sh might be like this:

#/bin/bash

function hello () {
    echo "hello world"
}

Then in my init.sh script it will do source hello.sh and I can call the hello function from terminal.

What is recommended?

I made this set up and it works, but it was when I didn't know a lot. I now know the common way to do things is by using symlinks. Is there anyone that can share some insight about why one way is better than another? Or is there another way that only code ninjas know?

Discussion (4)

Collapse
flrnd profile image
Florian Rand

First of all, welcome to the command line addicts club! I'd never thought using dropbox for that, I must say it's not a bad idea!

Although it's very common to use GitHub / Gitlab / Bitbucket (your preferred git platform here) to store your dotfiles (and shell scripts). With git you can achieve the sync problem. Now about running your scripts, I'd advice creating a bin/ directory inside your home and symlinking your scripts there to call them like every other program, ln -s /route/to/your/script $HOME/bin/somecoolscript.sh.

I have a .dotfiles directory on my home that I sync with github, then link files from this directory to their respective place.

I did a fast search and this article may be of help. If you have any doubts just ask!

Collapse
cookrdan profile image
Dan Author

Florian thank you! Yes I tend to keep a lot of things in dropbox/icloud - makes a new setup go quite fast.

I have had this spinning around in my head now since I posted this, and I believe what I will do is symlink them. I need to organize it in a slightly different way. I have seen so many ways people do this stuff and it doesn’t seem to matter but having a very organized way will be better in the long term. The author of the article also uses source for functions and aliases which is interesting. That made me feel like I’m not so far out to lunch.

I guess my big problem was that I wasn’t able to anticipate what I needed in the beginning and now that I’ve jumped in with both feet I need to change it. Fortunately I don’t have too much to change.

When I started I also didn’t know git very well and so I have spent a lot of time learning git and I need to also apply that to my setup.

I appreciate the link and your thoughts, thank you!

Collapse
flrnd profile image
Florian Rand

Anytime! I guess there is no defined or perfect way around it. If it works for you then I say go for it!

Collapse
taylorsilva profile image
Taylor Silva

Take a look at GNU stow maybe? I use it to symlink all my dot files