当 bash 脚本源自 tcsh 时,while 命令不起作用?

vik*_*kas 1 shell scripting bash tcsh csh

我制作了一个bash包含命令的 shell 脚本while,但是当我在终端上使用命令运行脚本时source,它给出了语法错误消息。

我需要使用source,因为我必须在终端上设置环境变量,没有source.

echo $shell给出:/bin/csh
shell 不是给出
的交互式输出ps -p $$CMD : tcsh

脚本是:

#! /bin/bash
i=1
while read line
do 
echo "$line $i"
echo
i=$((i+1))

done < seed.txt
Run Code Online (Sandbox Code Playgroud)

错误是:

i=1: Command not found.
while: Expression Syntax.
Run Code Online (Sandbox Code Playgroud)

ilk*_*chu 15

这个错误?

$ tcsh
tcsh> source while.sh 
i=1: Command not found.
while: Expression Syntax.
tcsh> exit
Run Code Online (Sandbox Code Playgroud)

Csh/tcsh 是与 POSIX sh 或 Bash 不同的 shell。尝试在 (t)csh 中以 sh 语法运行脚本是行不通的。

我需要使用source,因为我必须在终端上设置环境变量,没有source.

使其成为实际导出的环境变量setenv

tcsh> cat hello.sh 
echo "hello, $name"
tcsh> bash hello.sh
hello, 
tcsh> setenv name vikas
tcsh> bash hello.sh
hello, vikas
Run Code Online (Sandbox Code Playgroud)