使用' tr'在bash脚本中

时间:2014-11-15 12:44:22

标签: bash

如何在Bash脚本中使用tr '[a-z]' '[A-Z]'

我希望使用$1tr '[a-z]' '[A-Z]'保存到文本文件并打印到控制台。

我该怎么做?

#!/bin/bash  
echo $1 | tr '[a-z]' '[A-Z]'
exit 0

无效。

2 个答案:

答案 0 :(得分:0)

您可以使用命令tee

示例:

echo "AbC" | tr '[a-z]' '[A-Z]' | tee output.txt

它将在终端和文件(output.txt)中打印ABC

答案 1 :(得分:0)

Bash≥4时,您不需要tr转换为大写,因为您可以使用参数扩展:${var^^}将扩展为var的大写扩展

#!/bin/bash

# Convert first argument to upper case and save in variable upper1
upper1=${1^^}

# print to console:
printf '%s\n' "$upper1"

# and save to file
printf > file.txt '%s\n' "$upper1"

所有这些都是在没有外部工具的纯Bash中完成的。此外,不需要管道和子壳。