I have created a little script to add bookmarks in Nautilus to redirect users to network share. If I run the script manually, it works brillantly but as soon as I try to add it in /etc/profile.d, I can't connect to the computer. It seems that as soon as I do a loop or anything "fancy" like that, the script give an error. Here is an example if my currently working script :


# Creating gtk-bookmarks if it doesn't exists
if [[ ! -f ~/.gtk-bookmarks ]]
        touch ~/.gtk-bookmarks

# Adding bookmarks if not present
if ! grep -Fxq "smb://example.com/R03C01 Software:" ~/.gtk-bookmarks
    echo "smb://example.com/R03C01 Software:" >> ~/.gtk-bookmarks

As soon as I had a function like this one :

# Function find_server_by_ip()
# Desc : This function find the server to use for the current host IP Address
# Parameters : None
# Return : echo Server to use
function find_server_by_ip()
        hostname_ip=$(hostname -I)
        set -- $hostname_ip

        if [[ $3 -eq 78 ]]
                echo "Server A"
                echo "Server B"

...login stop working. What baffles me is that running this script with *bash add_shortcut.sh* or *./add_shortcut.sh* works fine...

Is it possible that we can't create functions, parse arrays or anything like that in a script called by /etc/profile? And in that case, where or how can I execute a login script for all the users connecting to the computer? Oh, by the way I am running Ubuntu 12.04 LTS!

Thanks in advance and sorry for my english!

EDIT : There is a pastebin of the script. I don't add it compeletly since the script comments are in french so...well, there it is : http://pastebin.com/71XzaBk6.


Stuff in /etc/profile.d is probably sourced by your window manager when you log in, and that probably uses /bin/sh, so target a POSIX shell, not bash, for scripts to be added there. Note that [[ is not found in the scripts that are in there now.


  • choose [ ... ] over [[ ... ]]
  • use funcname() { ... } without the function keyword

dash is a POSIX (only) shell, so you might find the dash man page handy

Comments on http://pastebin.com/71XzaBk6

  • This is the primary error: Change

    if [[ ! -f ~/.gtk-bookmarks ]]


     if [ ! -f ~/.gtk-bookmarks ]
  • the find_server_by_ip function can be simplified:

    findserverbyip() {
        case "$(hostname -I)" in 
            *[0-9].33.9.[0-9]*) share_server=serverB ;;
            *)                  share_server=serverA ;;

    Also, this comment is misleading: the function returns nothing, it has a side effect.

    # Return : echo Server to use
  • might as well remove the shebang line: this script doesn't get executed, it gets sourced.

| improve this answer | |
  • Note that goes for your ~/.profile too. – glenn jackman Mar 19 '14 at 20:37
  • Thanks for the fast reply! Unfortunately I tried to remove everything not "sh"-compliant and still the same problem. I'm starting to think that the problem is caused by /etc/profile that "source" the script, not execute them alongside himself...?? – Patrick Pruneau Mar 19 '14 at 20:51
  • show (or link to) the whole script. – glenn jackman Mar 19 '14 at 20:57
  • I added a link to pastebin with the (almost) whole script in the question. – Patrick Pruneau Mar 20 '14 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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