Where can I define functions, variables to be used in .desktop entries?
in ~/.bashrc, I define functions, variables and aliases
and export them, functions with # export -f fnname
I notice the exported functions are not known in .desktop entries
AFAIK the place to add aliases is in ~/.alias .
I am really not an expert in .desktop files, but reading about them, I do not understand where you think that shell (which shell) functions and variables are applicable in the Name=parameter definitions.
Maybe you explain better what your goal is according to http://www.catb.org/~esr/faqs/smart-questions.html#goal , so people can think along your lines.
In a desktop entry I have
Exec=/path/to/script.sh
In that script, the functions that I defined and exported in ~/.bashrc are not recognized, while they are recognized in teminal.
I just copied a function definition to /~.alias. After logout/login no change.
I remember to have read somewhere, /~.alias technically is same as ~/.bashrc. I guess executed along with ~/.bashrc, right before or after.
Hi
I suspect you would need to source ~/.bashrc in your script.sh since AFAIK Terminal=false in desktop files.
I did say NOTHING about putting functions in ~/.alias, I only said that that is the “normal” place to put aliases.
To me it looks as if /path/to/script.sh is NOT what you have there. Not telling what you really have is not helping your helpers. :(.
So, what is /path/to/script.sh?
ls -l /path/to/script.sh
And when it is a script of some sort (somewher your naming seems to point at that), then please show
head /path/to/script.sh
.bashrc is read by interactive shell and shell started to execute script is not interactive by definition.
well, at least, in the exec line it works. Probably not what the OP asked for but anyway. Also, I sincerely dislike shell env vars. Don’t the desktop environments have a proper k,v store?
There is an odd mix between the .desktop native % vars and shell $ vars. Here is an old desktop file of mine that extracted audio from video files
[Desktop Action convertToMP3m]
Name=Convert To MP3 Music
Icon=video-mpeg
Exec= tomp3="%f" && ffmpeg -i "$tomp3" -codec:a libmp3lame -qscale:a 2 -metadata title="`basename "${tomp3%.*}"`" "${tomp3%.*}".mp3
and this one for scaling images
[Desktop Action scaleTo1200]
Name=Scale Image to 1200px
Icon=image-jpeg
Exec=convert -strip -scale 1200x1200 %f "%d/email-`basename %f`"
I created those for my mom
I admit that it would be nice if we knew what the OP wants to achieve. I have no ideas if your examples fit there or not.
@hcvv I refrained explaining in public out of lazyness/efficiency and desire of security and keeping things simple for myself and you helpers. But I am willing to take a risk to a degree.
In /etc/profile.local I start syndaemon, which blocks my touchpad for 2 seconds when I type on my keyboard.
Running minecraft I want to pause syndaemon.
My changes at the moment:
/etc/profile.local
syndaemon -d -p ~/.syndaemon.pid # . syndpid
~/.bashrc
EdDe=/my/private/path
. $EdDe/syndfn.sh
/usr/share/applications/minecraft-launcher.desktop
#changelist
# . mc: start with $EdDe/mc.sh, which pauses syndaemon
[Desktop Entry]
Type=Application
Version=1.0 # o mc
Name=Minecraft Launcher
Comment=Official Minecraft Launcher
Exec=/my/private/path/mc.sh
# . mc Exec=/opt/minecraft-launcher/minecraft-launcher
Path=/opt/minecraft-launcher/
Icon=minecraft-launcher
Terminal=false
Categories=Game;Application;
$EdDe/mc.sh
#!/bin/bash
#$EdDe/mc.sh
. $EdDe/syndfn.sh
psynd
/opt/minecraft-launcher/minecraft-launcher &
pidmc=$!
set >>~/.syndaemon.pid # . debug
wait $pidmc
csynd
# . debug echo continued >>~/.syndaemon.pid
$EdDe/syndfn.sh
psynd () { kill -STOP `cat /home/ed/.syndaemon.pid` ; }
csynd () { kill -SIGCONT `cat /home/ed/.syndaemon.pid` ; }
export -f psynd csynd
While testing and developing I noticed, that in $EdDe/mc.sh, the variable EdDe from ~./bashrc is found, but no aliases, nor functions.
At first I had psynd, csynd defined as aliases, which failed.
I remember having read in the documentation, at this point not exactly where, that functions are meant to replace aliases.
But also the functions were not found in $EdDe/mc.sh
At that point I started this thread.
My changes work fine now.
I presumed that ~/.bashrc was called once, and that the namespace was copied.
But now I noticed that the namespace is rebuilt, and that variables, aliases and functions have their own namespaces. Only the variables namespace is available in scripts started by .desktop files with ‘Terminal=false’.
Thanks for the further explanation.
I must admit that I can not really follow your path of thought from begin to end (I e.g. wonder while the system manager puts something into /etc/profile.local, which is by definition something to be experienced by all users whenever they start a loginshell).
In any case, As you wonder why we ask you for pertinent, precise and exact information on what you are doing, having and getting, maybe some explanation. I already pointed you to a particular paragraph of http://www.catb.org/~esr/faqs/smart-questions.html#goal . The whole article is worth reading. Not everything there is applicable to the openSUSE forums, but many of the ideas there are. All here are volunteers spending some spare time in trying to help their fellow openSUSE users. But even spare time is not abundant for most. So there is a chance that people choose those threads for helping that offer a nice problem, with a good discussion. Asking for each and every bit of information is not a good discussion for many. It is simply spoiled time. When this was payed support, you would no doubt find that time specified on your bill.
While we understand that you suppress e.g. passwords and the like from being published here open on the internet, you must weigh off the amount of information you want to provide against the willingness of the people to help you. More paranoia: less help. People do not like it to provide help based on assumptions and the conclusions an OP may have jumped to based on not shown computer facts. In short: often people here will not believe you, they will believe the computer (and they trust you in showing what the computer showed you, unabridged, unaltered, except when you explicit and very clear tell what you left out).
=======================
Let we go more technical.
The following may be (partly) known by you. In any case some people above (including me) got the idea that you then do not fully understand the consequences while designing the solution to your goal.
A process is a running instance of a program. Each process has, apart from the memory it uses for code and data, a piece of memory call “process environment”. Amongst other things, there can be named environment variables with their value in the process environment. A process may start a new process, a child. The environment of the child is enherited from the parent. Thus environment variables and their value are forwarded from parent process to child process, but never the other way.
Very short without much details.
bash is a program interpreter and in Unix/Linux those programs are called scripts. A bash script uses (like every programming language) variables which can have values. For many there is a confused relation between the variables used in the script and the process variables that belong to the bash process that runs the script. That confusion derives from the fact that one most often see no difference in setting the value of such a variable, nor in using it. Example
AAP="noot"
echo $AAP
gives no clue if AAP is only inside the script or also known in the environment. But it is possible to e.g. move a variable from the running script to the environment: the export command.
export AAP
will put AAP in the process environment (with it’s value noot) and thus will not only be available to the running script, but also to child processes started by a command in the script that follows. E.g. assuming that mies is a executable (found using PATH) when now calling
mies wim
the program mies will be able to use the environment variable AAP getting the value noot.
An alias is to create a new command, often as a shortcut to another command. Some of the aliases provided by default:
alias beep='echo -en "\007"'
and for those with MS-DOS traumas:
dir='ls -l'
The bash man page explains very clear when alias are interpreted by the shell (on “simple commands”) and if one can use aliases of aliases.
There is a convention that a user can add aliases for her/his convenience by putting them in ~/.alias. This is because ~/.bashrc has the line:
test -s ~/.alias && . ~/.alias || true
In other words personal aliases are added when ~/.bashrc is sourced.
A function is something different. It is a typical feature of a programming language. It is a form of re-use of code. They can be very large. They can apparently also be exported to the environment and thus available to child processes. But that is only useful if the child process runs a script of the same language (bash script)
I see no relation between aliases and functions and certainly not that the one is going to replace the other.
Further read from the bash man page you could do is about INVOCATION, which will tell you about login shell (or not) and interactive shell (or not).
And remind that your executable program in /my/private/path/mc.sh will only be interpreted by bash because of the shebang #!/bin/bash
HTH
I found the text about replacing aliases by functions in man bash, at the end of paragraph ALIASES, ‘For almost every purpose, aliases are superseded by shell functions.’
I guess it is meant to explain, not to announce that aliases will deprecate.
Thanks, Henk.