0

I am trying to alias mytest='/usr/bin/python3.6'.

And, it can work when I type mytest in terminal, I can use python3.6 manually.

However, when I tried to run in script, it would go out some import errors.

My script like this: mytest test.py $1 $2 Run on bash test.sh /home/data1 /home/data2 Command like this image

Achaca
  • 85
  • 1
  • 5
  • Can you post the actual alias line you are having issue with? – Stephan Jun 04 '18 at 16:12
  • 3
    BTW, not directly related to your problem, but `mytest test.py $1 $2` is generally buggy -- if your `$1` and `$2` values contain spaces they can be split into multiple arguments; if they contain no characters not present in `IFS` they can simply be ignored outright; etc. Much better to use `"$1" "$2"` if you really want to pass exactly those two strings, or `"$@"` if you want to pass through the whole argument list. – Charles Duffy Jun 04 '18 at 16:24
  • 3
    Bigger issue, though -- aliases aren't supported in scripts by default at all. They're an interactive facility -- POSIX-compliant shells without extensions for interactive use aren't even required to support them. – Charles Duffy Jun 04 '18 at 16:25
  • 1
    If you used an exported function -- `mytest() { /usr/bin/python3.6 "$@"; }; export -f mytest` -- *that* would be supported in scripts with a bash shebang (`/bin/sh` is not guaranteed to honor exported functions, as they're a bash extension), though it's better practice to use an actual script called `mytest` at a location in the PATH. – Charles Duffy Jun 04 '18 at 16:26
  • I added my situation about my server. Thanks a lot – Achaca Jun 04 '18 at 17:25
  • Please post content as text, not graphical screenshots, wherever possible to do so. See https://meta.stackoverflow.com/a/285557/14122 – Charles Duffy Jun 04 '18 at 17:27
  • @Achaca, also, running `sh yourscript` means it's running with `sh`, not `bash`, even if the shebang at the top of the script is `#!/bin/bash`. As I said above, you need to use `bash` for exported functions to be honored (and, well, you need to actually define an exported function, not an alias). – Charles Duffy Jun 04 '18 at 17:28
  • okay got it, thanks a lot – Achaca Jun 04 '18 at 18:09

0 Answers0