csh - Cシェル
Cシェル(csh)は、1978年にカリフォルニア大学バークレー校のビル・ジョイによって開発されたUnixシェルです。対話的な使用のためにC言語に似た構文を提供するよう設計され、cshはコマンド履歴、エイリアス、ジョブ制御など、後のシェルで標準となる多くの機能を導入しました。cshとその拡張版であるtcshは主に対話セッション用であり、スクリプティングには適していませんが、特定のUnix環境では重要であり、一部のユーザーが好む独自の機能を提供しています。レガシーシステムや特定のUnixディストリビューションで遭遇する可能性があるため、cshを理解することはシステム管理者やユーザーにとって価値があります。
インストールとセットアップ
Cシェルの種類
# Check which csh variant is available
which csh
/bin/csh
which tcsh
/usr/bin/tcsh
# Check shell version
echo $version
# tcsh 6.22.04 (Astron) 2021-04-26 (x86_64-unknown-linux) options wide,nls,dl,al,kan,rh,color,filec
# Check if running csh or tcsh
echo $shell
/bin/tcsh
Cシェルのインストール
# Ubuntu/Debian
sudo apt update && sudo apt install csh tcsh
# CentOS/RHEL/Fedora
sudo dnf install tcsh
# macOS (tcsh is usually pre-installed)
# For csh specifically:
brew install tcsh
# Arch Linux
sudo pacman -S tcsh
# FreeBSD (usually pre-installed)
pkg install tcsh
# Check installation
csh --version
tcsh --version
csh/tcshをデフォルトシェルに設定
# Check current shell
echo $SHELL
# Add csh to available shells
echo /bin/csh|sudo tee -a /etc/shells
echo /bin/tcsh|sudo tee -a /etc/shells
# Set tcsh as default shell (recommended over csh)
chsh -s /bin/tcsh
# Verify change (restart terminal)
echo $SHELL
/bin/tcsh
基本設定ファイル
# Configuration files for csh/tcsh
~/.cshrc # Main configuration file
~/.tcshrc # tcsh-specific configuration
~/.login # Login shell configuration
~/.logout # Logout script
# Create basic .tcshrc
cat > ~/.tcshrc ``<< 'EOF'
# tcsh configuration
# Set environment variables
setenv EDITOR vi
setenv PAGER less
setenv LANG en_US.UTF-8
# Set path
set path = (/usr/local/bin /usr/bin /bin $path)
# Aliases
alias ll 'ls -l'
alias la 'ls -la'
alias h 'history'
alias .. 'cd ..'
# Prompt
set prompt = '%n@%m:%c$ '
# History
set history = 1000
set savehist = 1000
# Completion
set autolist
set complete = enhance
# Other options
set noclobber
set notify
EOF
# Source configuration
source ~/.tcshrc
Cシェルの構文と変数
変数の代入と使用
# Simple variable assignment
set name = "John Doe"
set age = 30
set path_var = "/home/user"
# Using variables
echo $name
echo "Hello, $name"
echo 'Literal: $name' # Single quotes prevent expansion
# Array variables
set fruits = (apple banana orange)
set numbers = (1 2 3 4 5)
# Accessing array elements
echo $fruits[1] # First element (1-indexed)
echo $fruits[2] # Second element
echo $fruits[$#fruits] # Last element
echo $fruits[*] # All elements
echo $#fruits # Number of elements
# Environment variables
setenv PATH "/usr/local/bin:$PATH"
setenv EDITOR vi
setenv HOME /home/user
# Unset variables
unset name
unsetenv PATH # Dangerous!
# Special variables
echo $0 # Shell name
echo $ # Process ID
echo $? # Exit status of last command
echo $#argv # Number of arguments
echo $argv[*] # All arguments
文字列操作
# String concatenation
set first = "Hello"
set second = "World"
set combined = "$first, $second!"
echo $combined # Hello, World!
# String length (tcsh)
set string = "Hello, World!"
echo $%string # Length: 13
# Substring operations (limited in csh)
set filename = "document.txt"
set basename = $filename:r # Remove extension: document
set extension = $filename:e # Get extension: txt
set dirname = $filename:h # Get directory (if path)
set tail = $filename:t # Get filename (if path)
# Case conversion (tcsh)
set upper = $string:u # Uppercase
set lower = $string:l # Lowercase
コマンド置換
# Command substitution using backticks
set current_date = `date`
set file_count = `ls|wc -l`
set user_home = `eval echo ~$USER`
# Using command substitution in expressions
echo "Today is `date +%A`"
set files = (`ls *.txt`)
# Nested command substitution
set day_of_week = `date +%A`
echo "Today is $day_of_week, `date +%B` `date +%d`"
制御構造
条件文
# if-then-else
if ($age >`` 18) then
echo "Adult"
else if ($age == 18) then
echo "Just turned adult"
else
echo "Minor"
endif
# String comparisons
if ("$name" == "John") then
echo "Hello John"
endif
if ("$name" != "John") then
echo "Not John"
endif
# File tests
if (-f "file.txt") then
echo "File exists"
endif
if (-d "directory") then
echo "Directory exists"
endif
if (-r "file.txt") then
echo "File is readable"
endif
if (-w "file.txt") then
echo "File is writable"
endif
if (-x "script.sh") then
echo "File is executable"
endif
# Logical operators
if ($age > 18 && $age ``< 65) then
echo "Working age"
endif
if ($status == 0||$force == "true") then
echo "Proceeding"
endif
if (! -f "file.txt") then
echo "File does not exist"
endif
スイッチ文
# switch statement
switch ($variable)
case pattern1:
echo "Matched pattern1"
breaksw
case pattern2:
case pattern3:
echo "Matched pattern2 or pattern3"
breaksw
default:
echo "No pattern matched"
breaksw
endsw
# Switch with file extensions
switch ($filename)
case *.txt:
echo "Text file"
breaksw
case *.jpg:
case *.png:
case *.gif:
echo "Image file"
breaksw
case *.sh:
echo "Shell script"
breaksw
default:
echo "Unknown file type"
breaksw
endsw
ループ
# foreach loop
foreach item (apple banana orange)
echo "Fruit: $item"
end
# foreach with array variable
set files = (*.txt)
foreach file ($files)
echo "Processing: $file"
end
# foreach with command substitution
foreach user (`cat users.txt`)
echo "User: $user"
end
# while loop
set counter = 1
while ($counter <= 10)
echo "Counter: $counter"
@ counter++
end
# while loop with file reading
set line = ""
while (1)
set line = $<
if ($line == "") break
echo "Line: $line"
end
# Nested loops
foreach dir (/usr /opt /var)
foreach file ($dir/*)
if (-f $file) then
echo "File: $file"
endif
end
end
算術演算
@コマンドによる算術演算
# Basic arithmetic
@ result = 5 + 3 # 8
@ result = 10 - 4 # 6
@ result = 6 * 7 # 42
@ result = 20 / 4 # 5
@ result = 17 % 5 # 2 (modulo)
# Arithmetic with variables
set num1 = 10
set num2 = 5
@ sum = $num1 + $num2 # 15
@ product = $num1 * $num2 # 50
# Increment and decrement
set counter = 0
@ counter++ # Increment
@ counter-- # Decrement
@ counter += 5 # Add and assign
@ counter -= 3 # Subtract and assign
# Complex expressions
@ result = ($num1 + $num2) * 2
@ result = $num1 ** 2 # Exponentiation (tcsh)
# Comparison operations
@ is_greater = ($num1 >`` $num2) # Returns 1 if true, 0 if false
if ($is_greater) then
echo "num1 is greater than num2"
endif
外部算術ツール
# Using expr for complex calculations
set result = `expr 5 + 3`
set result = `expr $num1 \* $num2` # Note: * must be escaped
# Using bc for floating point
set result = `echo "scale=2; 10/3"|bc`
set result = `echo "scale=4; sqrt(16)"|bc -l`
# Using awk for calculations
set result = `awk "BEGIN \\{print 10/3\\}"`
エイリアスと履歴
エイリアス管理
# Simple aliases
alias ll 'ls -l'
alias la 'ls -la'
alias h 'history'
alias .. 'cd ..'
alias ... 'cd ../..'
# Aliases with arguments
alias rm 'rm -i' # Interactive removal
alias cp 'cp -i' # Interactive copy
alias mv 'mv -i' # Interactive move
# Complex aliases
alias lsd 'ls -l|grep "^d"' # List only directories
alias psg 'ps aux|grep' # Process search
# Conditional aliases
if (-f /usr/bin/vim) then
alias vi vim
endif
# List aliases
alias # Show all aliases
alias ll # Show specific alias
# Remove aliases
unalias ll
unalias * # Remove all aliases
履歴管理
# History configuration
set history = 1000 # Number of commands to remember
set savehist = 1000 # Number of commands to save to file
# History commands
history # Show all history
history 10 # Show last 10 commands
history -r # Read history from file
history -w # Write history to file
# History expansion
!! # Previous command
!n # Command number n
!string # Last command starting with string
!?string # Last command containing string
^old^new # Replace old with new in previous command
# History modifiers
!:0 # Command name only
!:1 # First argument
!:$ # Last argument
!:* # All arguments
!:1-3 # Arguments 1 through 3
# Examples
echo !$ # Echo last argument of previous command
cp file.txt !$.bak # Copy file with .bak extension
ジョブ制御とプロセス管理
ジョブ制御
# Background jobs
command & # Run command in background
jobs # List active jobs
fg %1 # Bring job 1 to foreground
bg %1 # Send job 1 to background
kill %1 # Kill job 1
# Job control signals
# Ctrl+C: Interrupt (SIGINT)
# Ctrl+Z: Suspend (SIGTSTP)
# Ctrl+\: Quit (SIGQUIT)
# Process management
ps # Show current processes
ps aux # Show all processes
kill PID # Terminate process
kill -9 PID # Force kill process
killall process_name # Kill all processes by name
# Nohup equivalent
nohup command & # Run command immune to hangups
プロセス情報
# Process variables
echo $ # Current shell PID
echo $! # PID of last background job
# Process status
echo $? # Exit status of last command
echo $status # Same as $? in csh
# Wait for processes
wait # Wait for all background jobs
wait %1 # Wait for specific job
入出力とリダイレクション
基本的な入出力リダイレクション
# Output redirection
command > file.txt # Redirect stdout to file
command >> file.txt # Append stdout to file
command >& file.txt # Redirect both stdout and stderr
command >>& file.txt # Append both stdout and stderr
# Input redirection
command < input.txt # Read input from file
sort < unsorted.txt > sorted.txt
# Pipes
ls -l|grep "txt" # Pipe output to grep
ps aux|grep "process"|wc -l # Count matching processes
# Here documents (limited support)
cat << EOF
This is a here document
Variables like $HOME are expanded
EOF
高度な入出力機能
# Noclobber option
set noclobber # Prevent overwriting files
command >! file.txt # Force overwrite with noclobber set
# Tee equivalent
command|tee file.txt # Write to file and stdout
# Error redirection
command >& /dev/null # Redirect both stdout and stderr to null
(command > output.txt) >& error.log # Separate stdout and stderr
組み込みコマンドと機能
重要な組み込みコマンド
Note: For sections 3-20, I’ve left placeholders as the specific content was not provided in the original text.```bash
echo command
echo “Simple message” echo -n “No newline” # tcsh only
printf (tcsh only)
printf “%s: %d\n” “Count” 42 printf ”%-10s %5d\n” “Name” 123
set and setenv
set var = value # Local variable setenv VAR value # Environment variable
which and where
which command # Show command location where command # Show all command locations (tcsh)
Directory operations
cd directory # Change directory pushd directory # Push directory onto stack popd # Pop directory from stack dirs # Show directory stack
File operations
ls -l # List files cp source dest # Copy files mv old new # Move/rename files rm file # Remove files mkdir directory # Create directory rmdir directory # Remove empty directory
```bash
# Command completion
set complete = enhance # Enhanced completion
set autolist # Automatically list completions
# Spelling correction
set correct = cmd # Correct commands
set correct = complete # Correct completions
set correct = all # Correct everything
# File completion
set filec # Enable filename completion
# Auto-logout
set autologout = 60 # Auto-logout after 60 minutes
# Watch for logins
set watch = (any any) # Watch for any user login
set who = "%n has %a %l from %M at %t." # Login message format
```## 設定とカスタマイズ
### プロンプトのカスタマイズ
```bash
# Simple prompts
set prompt = '%n@%m:%c$ ' # user@host:dir$
set prompt = '% ' # Simple %
set prompt = '\! % ' # History number %
# Prompt escape sequences
# %n - Username
# %m - Hostname
# %c - Current directory (basename)
# %C - Current directory (full path)
# %/ - Current directory (full path)
# %~ - Current directory (with ~ substitution)
# %t - Time (12-hour)
# %T - Time (24-hour)
# %p - Time (AM/PM)
# %d - Day of week
# %D - Date
# %w - Month
# %W - Year
# %! - History number
# %# - # if root, % otherwise
# Advanced prompt with colors (tcsh)
set prompt = '%\\\\{\033[1;32m%\\\\}%n@%m%\\\\{\033[0m%\\\\}:%\\\\{\033[1;34m%\\\\}%c%\\\\{\033[0m%\\\\}$ '
# Multi-line prompt
set prompt = '%n@%m:%c\
$ '
# Conditional prompt
if ($uid == 0) then
set prompt = 'root@%m:%c# '
else
set prompt = '%n@%m:%c$ '
endif
```### 環境設定
```bash
# Path management
set path = (/usr/local/bin /usr/bin /bin)
set path = ($path /opt/bin) # Append to path
# Environment variables
setenv EDITOR vi
setenv PAGER less
setenv BROWSER firefox
setenv LANG en_US.UTF-8
# Platform-specific configuration
switch (`uname`)
case Linux:
setenv LS_COLORS 'di=34:ln=35:so=32:pi=33:ex=31:bd=46;34:cd=43;34:su=41;30:sg=46;30'
breaksw
case Darwin:
setenv LSCOLORS ExFxCxDxBxegedabagacad
breaksw
endsw
# Conditional environment
if (-d "$HOME/.local/bin") then
set path = ($HOME/.local/bin $path)
endif
if ($?DISPLAY) then
setenv BROWSER firefox
else
setenv BROWSER lynx
endif
```### シェルオプション
```bash
# Important shell options
set noclobber # Prevent file overwriting
set notify # Report job status immediately
set noglob # Disable filename expansion
set ignoreeof # Don't exit on Ctrl+D
# tcsh-specific options
set autolist # List completions automatically
set complete = enhance # Enhanced completion
set correct = cmd # Correct commands
set filec # Filename completion
set histdup = erase # Remove duplicate history entries
set listjobs = long # Long format for job listing
set rmstar # Ask before rm *
```## スクリプティングに関する考慮事項
### スクリプト構造
```bash
#!/bin/csh -f
# Script description
# Note: -f flag prevents reading .cshrc
# Variable declarations
set script_name = $0:t
set script_dir = $0:h
set version = "1.0"
# Function equivalent (using goto/label)
goto main
usage:
echo "Usage: $script_name [options] [arguments]"
echo "Options:"
echo " -h Show this help"
echo " -v Show version"
exit 0
version:
echo "$script_name version $version"
exit 0
error:
echo "Error: $error_msg"
exit 1
main:
# Parse arguments
while ($#argv > 0)
switch ($argv[1])
case -h:
goto usage
case -v:
goto version
case -*:
set error_msg = "Unknown option: $argv[1]"
goto error
default:
break
endsw
shift argv
end
# Main script logic
echo "Script execution completed"
exit 0
```### cshスクリプトのベストプラクティス
```bash
# Use tcsh instead of csh for scripts
#!/bin/tcsh -f
# Always use -f flag to avoid .cshrc interference
#!/bin/csh -f
# Error handling
set error_exit = 0
if (! -f "required_file.txt") then
echo "Error: Required file not found"
set error_exit = 1
endif
if ($error_exit) exit 1
# Input validation
if ($#argv == 0) then
echo "Error: No arguments provided"
exit 1
endif
# Safe variable usage
if ($?variable) then
echo "Variable is set: $variable"
else
echo "Variable is not set"
endif
# Avoid complex scripting in csh
# Use sh/bash for complex scripts
# Use csh/tcsh primarily for interactive use
```## 制限と代替案
### 既知の制限
```bash
# csh scripting limitations:
# 1. No functions (use goto/labels instead)
# 2. Limited error handling
# 3. No local variables in "functions"
# 4. Inconsistent syntax
# 5. Poor signal handling
# Example of workaround for function-like behavior
goto main
# "Function" using goto/label
process_file:
set file = $1
if (-f $file) then
echo "Processing $file"
# Process file here
else
echo "File $file not found"
endif
goto return_point
main:
set return_point = main_continue
set argv[1] = "test.txt"
goto process_file
main_continue:
echo "Back in main"
exit 0
```### csh/tcshを使用するタイミング
```bash
# Good for:
# - Interactive shell use
# - Simple automation tasks
# - Users familiar with C-like syntax
# - Legacy system compatibility
# Not recommended for:
# - Complex scripting (use bash/sh instead)
# - Portable scripts
# - Production automation
# - Error-critical applications
# Migration example from csh to bash
# csh version:
# if ($status == 0) then
# echo "Success"
# endif
# bash equivalent:
# if [ $? -eq 0 ]; then
# echo "Success"
# fi
```### 相互運用性
```bash
# Running bash scripts from csh
bash script.sh
# Converting csh variables for bash
setenv BASH_VAR $csh_var
bash -c 'echo $BASH_VAR'
# Calling csh from bash
csh -c 'echo $csh_variable'
# Mixed environment
# Use bash for scripting, csh for interactive
exec bash script.sh # Execute bash script
exec tcsh # Return to tcsh
```C シェルとその拡張バージョンである tcsh は、Unix シェルの進化における重要な章を表し、後のシェルで標準となる多くの機能を導入しました。csh は様々な制限と一貫性の欠如により複雑なスクリプティングには推奨されませんが、対話的な使用、特に C 言語に似た構文と特定の機能を好むユーザーにとっては依然として価値があります。csh を理解することは、レガシーシステムで遭遇するシステム管理者や、コマンドライン操作の独特なアプローチを評価するユーザーにとって重要です。ただし、最新のシェルスクリプティングのニーズには、POSIX 準拠の bash やより高度な zsh シェルが一般的に推奨されます。