使用bash脚本设置python路径?
问题描述:
我有一个bash脚本,它执行一些特定的功能,并使用Python 3.5在本身内运行命令。我可以存储使用bash脚本设置python路径?
which python
在一个变量的输出,但我需要设置.bash_profile中指定的文件中,这样我可以在bash脚本中调用
python3.5 some_command
。
谢谢。
答
使用x="which python"
然后eval $x
。
在这里看到:How to store a command in a variable in Linux?
如果你想将其存储在一个文件中,可以随时拨打which python >> outfile
然后cat outfile
读的内容回
或者你可以使用sed
或awk
来代替一些文本在像.bash_profile
或其他任何配置文件。请参阅Find and Replace Inside a Text File from a Bash Command。
所以,如果有叫.bash_profile中PythonLocation
一个占位符的位置,你可以做
x="which python"
eval $x
sed -i -e 's/PythonLocation/$x/g' .bash_profile