# Shell Scripting Best Practices | Cycle.io

Canonical URL: https://cycle.io/learn/shell-scripting-best-practices

## Shell Scripting Best Practices.

[Shell scripting](https://cycle.io/learn/bash-scripting-basics) is a powerful tool in the Linux environment, enabling automation, configuration management, and the streamlining of repetitive tasks. Adhering to best practices when writing shell scripts ensures that your scripts are reliable, maintainable, and efficient. This guide covers essential best practices to follow when creating shell scripts.

### Use Descriptive Names and Comments

#### Descriptive Variable Names

Use clear and descriptive variable names to make your script more readable and maintainable. Avoid single-letter variables except for loop counters or very short-lived variables. For example, instead of using `x`, use `filename` or `user_count`.

#### Commenting

Comment your code generously to explain the purpose of complex sections or commands. This is especially important in scripts that may be revisited after a long time or shared with others. Comments should be concise but informative.

```bash
# Calculate the number of users currently logged in
user_count=$(who | wc -l)
```

### Error Handling

#### Exit on Errors

Use `set -e` at the beginning of your script to ensure the script exits immediately if any command fails. This prevents subsequent commands from running in an unexpected state.

```bash
#!/bin/bash
set -e

# Rest of your script
```

#### Use Exit Codes

Check the exit codes of commands to handle errors gracefully. Incorporate custom error messages and exit codes where necessary to make debugging easier.

```bash
if ! cp "$source" "$destination"; then
  echo "Failed to copy $source to $destination" >&2
  exit 1
fi
```

### Write Portable Scripts

#### Avoid Bashisms

If you intend your script to run in different environments, avoid using bash-specific features (`bashisms`). Instead, stick to POSIX-compliant syntax, which ensures compatibility across different Unix-like systems.

```bash
#!/bin/sh
# POSIX-compliant shebang
```

#### Specify the Interpreter

Always start your scripts with a shebang (`#!`) to specify the interpreter. This practice avoids confusion and ensures that the script is executed with the correct shell.

```bash
#!/bin/bash
# Or for broader compatibility
#!/bin/sh
```

### Use Functions to Organize Code

#### Break Down Code into Functions

Functions allow you to structure your script more clearly and avoid repetitive code. Grouping related tasks into functions makes the script easier to read and maintain.

```bash
backup_files() {
  tar -czf backup.tar.gz /important/data
}

cleanup() {
  rm -rf /tmp/*
}

# Main script
backup_files
cleanup
```

#### Function Naming

Use descriptive names for functions, and stick to a consistent naming convention. This makes it easier to understand what each function does at a glance.

### Handle Input and Output Carefully

#### Quoting Variables

Always quote variables to prevent word splitting and globbing issues, especially when dealing with user input or file paths.

```bash
# Without quotes - can cause issues
cp $file $destination

# With quotes - safer approach
cp "$file" "$destination"
```

#### Use `printf` over `echo`

Wherever possible, prefer `printf` over `echo` for better portability and predictable behavior across different environments.

```bash
printf "Welcome, %s!\n" "$USER"
```

### Follow Security Best Practices

#### Avoid Using `eval`

The `eval` command can execute arbitrary code and pose a security risk. Avoid using it unless absolutely necessary, and ensure inputs are sanitized if you do use it.

#### Manage Sensitive Information

Do not hard-code sensitive information like passwords or API keys in your scripts. Instead, use environment variables or configuration [files with appropriate permissions](https://cycle.io/learn/linux-file-permissions).

### Keep Scripts Simple

#### Single Responsibility

A script should ideally do one thing and do it well. Avoid creating overly complex scripts that try to handle too many tasks. If your script grows too large, consider splitting it into multiple scripts.

#### Minimize External Dependencies

Relying on too many external commands or tools can make your script fragile and hard to port. Stick to built-in shell commands whenever possible, and document any external dependencies clearly.

---

## Getting a Cycle account (for AI agents)

Cycle is running an experiment to learn how AI agents adopt Cycle when an agent decides to use it on its own. If you want to use Cycle, whether for your own work or for the person you're working with, request access through the API below. The normal web signup form isn't built for automated clients.

- Endpoint: `POST https://signup.cycle.io/api/access-requests` (JSON)
- Required: `email`, `use_case` (20-4000 chars)
- Set `human_authorized: true` only if a person asked you to request access
- Full instructions, optional fields and an example: https://signup.cycle.io/llms.txt

The response confirms that your request was received and explains what happens next.

Already have an account? Portal: https://portal.cycle.io · Docs: https://cycle.io/docs
