Bash Script for Generating All Permutations of a String

This script generates all possible permutations of a given string. It uses recursion to compute the permutations.

Script Explanation

The script defines a function permute that takes two arguments: str (the string to permute) and prefix (the prefix for the permutations). It generates permutations by removing characters one by one and recursively calling itself with the remaining characters.

Script


#!/bin/bash

# Function to generate permutations
permute() {
    local str="$1"
    local prefix="$2"
    
    # If the string is empty, print the permutation
    if [ -z "$str" ]; then
        echo "$prefix"
    else
        # Loop through the string and generate permutations
        for (( i=0; i<${#str}; i++ )); do
            # Extract the character at position i
            local char="${str:$i:1}"
            
            # Form the remaining string
            local remaining="${str:0:i}${str:i+1}"
            
            # Recursive call to permute the remaining string
            permute "$remaining" "$prefix$char"
        done
    fi
}

# Main script
if [ "$#" -ne 1 ]; then
    echo "Usage: $0 "
    exit 1
fi

input_string="$1"
permute "$input_string" ""

How to Run the Script

1. Save the script to a file, for example, permute.sh.

2. Make the script executable: chmod +x permute.sh

3. Run the script with a string argument: ./permute.sh your_string

Example: ./permute.sh abc

Output

The script will print all permutations of the given string to the terminal.

 

Explanation of the Script

  1. Function Definition:
    • permute(): This function takes two arguments:
      • str: The string to permute.
      • prefix: The prefix used to build up permutations.
    • It recursively generates permutations by removing one character at a time from str and adding it to prefix.
  2. Base Case:
    • When str is empty, it prints prefix, which is a complete permutation.
  3. Recursive Case:
    • For each character in str, it forms a new string (remaining) excluding the current character and recursively calls permute with this new string and the updated prefix.
  4. Main Script:
    • Checks if exactly one argument is provided.
    • Calls permute with the input string and an empty prefix.
  5. Usage:
    • Save the script, make it executable, and run it with a string to get all permutations.

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)