---
title: "最大公约数的bash脚本"
author: "polyahu"
date: "2013-02-15T21:37:02Z"
tags: []
categories: ["编程语言"]
slug: "bash"
draft: false
lastmod: "2013-02-15T21:37:02Z"
id: 1798218
url: "https://polyahu.iteye.com/blog/1798218"
---

```bash

#!/usr/bin/bash
gcd() 
{
  until test 0 -eq "$2"; do
    set -- "$2" "`expr "$1" % "$2"`"
  done

  test 0 -gt "$1" && set -- "`expr 0 - "$1"`"
  echo "$1"
}
gcd $1 $2

```


此处的set命令用--的参数将$2和'expr $1 % %2'的值交换，比较简洁。