Expressions & Syntax

Reference for MXCMD literals, assignment, operators, redirection, and command substitution patterns.

Literals

Variables & Assignment

var_name = expression
--var_name    # decrement
var_name--    # decrement (postfix)
++var_name    # increment
var_name++    # increment (postfix)

Operators

Arithmetic: +, -, *, /, %

Unary: prefix -, ++, --; postfix ++, --

Pipelines & Redirection

Logical Operators

use && and || for boolean logic

pwd && pwd
ls || pwd
    

User-Defined Commands

define (param1, param2, ...)   # define a new command
begin 
    statements
    return 0  # return a value 0 success non zero failure
end   # end of definition

Command Substitutions

Command substitutions allow you to capture the output of a command and use it as a value. They are denoted by $(command) syntax.

Basic Usage

files = $(ls)
count = $(exec "wc -l < \"data.txt\"")
today = $(exec "date \"+%Y-%m-%d\"")

Using Command Substitutions as Arguments

Command substitutions can be used directly as arguments to other commands:

# Use ls output as an argument to another command
cat $(exec "ls | grep \"\.txt$\" | head -n1")

# Use command substitution in file operations
cp important.txt $(printf "backup_%s.txt" $(exec "date \"+%Y%m%d\""))

# Use in user-defined commands
list_files $(pwd)

Nested Command Substitutions

Command substitutions can be nested to create complex expressions:

d substitution for dynamic file naming
report = $(cat $(printf "logs/log_%s.txt" $(exec "date \"+%Y%m%d\"")))

# Complex example with multiple levels of nesting
fileext = $(exec $(printf "stat -f \"%%SB\" -t \"%%Y.%%m.%%d\" %s" $(exec "ls | head -n1")))

Command Substitution Processing

Example: Count Lines in Files with Command Substitution


define count_text(dir, ext)
begin
    total = 0.000000
    for f in $(ls | sort)
    do
        path = $(printf "%s/%s" dir f)
        pos = $(strfind 0.000000 path ext)
        if test pos --ne -1.000000
        then
            count = $(wc --l path)
            total = total + count
            printf "File: %s has %d lines\n" path count
        fi
    done
    printf "total lines: %d\n" total
end