2

What am I doing wrong...? It's fine if I do this on the command line and then call it but not when I load it from .profile. Linux Mint Qiana, Bash 4.*, if it matters.

function android() { command /opt/android-studio/bin/studio.sh "$@" & disown ; }

export -f android

I've tried shortening the command, extending it, removing the semi-colon and using a newline instead... I guess I haven't found the happy compromise yet. No errors when ran on the command line and the function does work as it currently is listed above.

Notes: By "load" I mean to open a new terminal session with the same user whose .profile I am editing... and I am using things like function, command and disown because I started with a bare-bones version of this function but it wasn't working so I started adding and removing stuff to try and get the correct combination of things. Everything ran fine on the command line.

6
  • What do you mean by "load it from .profile"?↵Why are you using the command function?
    – dan
    Jul 5 '15 at 7:59
  • @mikeserv, yes you are right. Sorry. Functions can be exported. It was a bit confusing, cause I cannot export -n android and I don't see it in export -p, while help export says, that export -p shows exported variables and functions.
    – ikrabbe
    Jul 5 '15 at 8:14
  • Actually the concept of exported functions is a bit strange. To get control over exported functions you can use the shell-builtin declare.
    – ikrabbe
    Jul 5 '15 at 8:25
  • Could you add to your OQ what you diagnose as wrong? Does your shell or one of your commands give an error message?
    – dan
    Jul 5 '15 at 10:13
  • whoa sorry just work up.... late night. by "load" i mean open a new terminal session. and i am using command because what i pasted above is the most "expanded" i could make the function.... like i described, i tried adding and removing things hoping some combination would work out. internet suggested the use of command so i gave it go. no issue on the command line so i went ahead and left it. obviously, i started with a barebones function but it didn't work. typing out disown isn't exactly typical either. Jul 5 '15 at 16:44
4

On Ubuntu and Linux Mint, most ways to start a GUI session, including I believe the default one, cause /etc/profile and ~/.profile to be read by /bin/sh, not by bash. /bin/sh is dash, a shell that is faster and uses less memory than bash, but lacks some of its features such as function export. Your ~/.xsession-errors must contain lines like .profile: 42: export: Illegal option -f.

Instead of putting your functions in the environment, define them in the initialization file for interactive shells: ~/.bashrc. There's no point in exporting them since the file is read by each interactive shell, and non-interactive shells will mostly be sh, not bash, so they won't import the function — and anyway disown isn't useful in a non-interactive shell.

3
  • Just curious.. is there a way to find out which shell is reading /etc/profile and ~/.profile ?
    – heemayl
    Jul 5 '15 at 23:13
  • 1
    @heemayl Read the display manager configuration and the session startup scripts, e.g. /etc/gdm3/Xsession. I don't know what the Mint default it. Or find it out experimentally: add ps lww $$ to ~/.profile and look at ~/.xsession-errors after logging in. Jul 5 '15 at 23:26
  • Thanks. I didn't know the difference between .profile and .bashrc. With a clean install, only .profile exists so I used it. Also, I assumed bash because it is my default shell and there was no shebang to say otherwise. I'm going to leave the other answer as answered because they got there first but I gave you a +1 b/c of infos. Jul 6 '15 at 14:21
0

Traditionally bash functions are placed in ~/.bashrc as this is read by interactive bashes. ~/.profile is only read by login bashes. New windows usually dont run login bashes.

2
  • ohhh man.... but.... I've stuck a lot of things in there to test and stuff. Why does everything else work? My $PATH totally updates for each new terminal session when I use .profile. I'd truly like it in .profile based on what you said, too. Jul 5 '15 at 17:51
  • bash seems to export functions by creating an environment variable BASH_FUNC_myfn for a function myfn. You could use the env command in your terminal to see if this is being lost or corrupted somehow. When you open new windows this environment variable passes through several programs. Perhaps one of them leaves PATH but removes other variables.
    – meuh
    Jul 5 '15 at 19:24

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.