CentOSのbashシェルにある〜/ .bash_profileファイルで関数を定義する正しい方法は何ですか?

CentOSのbashシェルにある〜/ .bash_profileファイルで関数を定義する正しい方法は何ですか?

CentOSでbashシェルを使用しています。 PostGres 14クエリを実行して結果を出力する関数を定義したいです。

eth_price 2023-12-06 10:05

私の~/.bash_profileファイルで私が定義した

eth_price() {
  timestamp="$1:00"  # Append ":00" to the provided timestamp
  PGPASSWORD=$DB_PASS psql -U $DB_USER -d $DB_NAME -c "select price from cryptocurrencyprice p, cryptocurrency c where c.id = p.crypto_currency_id and c.abbrev = 'ETH' and p.created <= '$timestamp' order by created desc limit 1;"
}

残念ながら、私が走るとき

source ~/.bash_profile

エラーが発生しました。

-bash: /home/myuser/.bash_profile: line 31: syntax error near unexpected token `('
-bash: /home/myuser/.bash_profile: line 31: `eth_price() {'

私の機能を定義する正しい方法は何ですか?

編集する:

バージョン出力です。

$ bash --version
GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

問題をさらに分離するために、以下のように完全な〜/ .bashrcファイルを作成しました。

# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=

# User specific aliases and functions
eth_price() {
  timestamp="$1:00"  # Append ":00" to the provided timestamp
  PGPASSWORD=$DB_PASS psql -U $DB_USER -d $DB_NAME -c "select price from price p, currency c where c.id = p.id and c.abbrev = 'ETH' and p.created <= '$timestamp' order by created desc limit 1;"
}

興味深いことに、これは失敗します。

$ source ~/.bashrc
-bash: /home/myuser/.bashrc: line 12: syntax error near unexpected token `('
-bash: /home/myuser/.bashrc: line 12: `eth_price() {'

しかし、効果がありました...

$ sh ~/.bashrc
$ 

ベストアンサー1

おすすめ記事