Why SHC?
First of all, why are you using SHC, given your "hobbyist" motivations? Here is an excerpt from their own description:
Upon execution, the compiled binary will decrypt and execute the code with the shell -c option. Unfortunatelly, it will not give you any speed improvement as a real C program would.
The compiled binary will still be dependent on the shell specified in the first line of the shell code (i.e. #!/bin/sh), thus shc does not create completely independent binaries.
SHC's main purpose is to protect your shell scripts from modification or inspection.
- My opinion (I'll keep it sort of brief): Even if your motivation is the stated "main purpose" of preventing modifications, a mildly determined person can still recover (and, hence, modify) the original script! SHC essentially provides security by obscurity, which is an oft-derided strategy when used as a primary means of security. If this doesn't sound helpful, then I'd recommend ditching SHC and simply using shell scripts as the vast majority of others do. If you need real security for your shell scripts, I'd suggest asking a specific question about that, without SHC or "compilers".
The specific $0 problem
I downloaded SHC 3.8.9 from this page just to try this out.
I was not able to reproduce the problem, on Ubuntu 14.04 LTS.
#!/bin/bash
echo "Hello, world. My name is \`$0'"
Test run
$ ./shc -f my_test.bash
$ ~/path/to/my_test.bash.x
Hello, world. My name is `~/path/to/my_test.bash.x'
So, clearly, our systems differ. I can try to update this answer if you post more details about your operating system, version of SHC, and specific shell script and shc
commandline you used to "compile" it.
Why do you need the path?
Why does your script need to know its path? Are you storing files with the script? Does it "default" to operate on the directory it was run in if the user doesn't specify? Whether these things are good ideas or not is a matter of opinion, but knowing your motivations might be helpful in narrowing down this answer.
How to fetch the current directory
The pwd
command fetches the current directory. In many cases, you can assemble the full path of your script (assuming it was run with an explicit relative path) with something like:
realname="`pwd`/$0"
... which would result in a value like:
/path/to/script/./yourscript.bash.x
The extra ./
just means "current directory", so while it might be cosmetically unfortunate, it will not negatively affect the result.
If the script is simply in your $PATH
, then you would need to use which
instead of pwd
, but we're already getting beyond the scope of the original question, here, so I'll conclude with a simple mention that determining pathnames can be a tricky process, especially if you need to do so in a secure manner, so that would best be left for another question with a specific problem statement along these lines.
$0
is bash syntax, not something you have in an executable. Are you using some kind of “compiler” for shell scripts? That's very unusual and basically never useful. And if you are, it's absolutely the sort of thing we can't guess, you need to say what tool you're using and how. And tell us how you're executing the script, too.sudo
, you can configure it to allow specified command to be run as specified user, without a password. (because I am guessing from your code that you are trying to hide a password it at executable, because you think that if you can not extract it then I don't know how to runstrings
)